n × n grid, and cell (i, j) holds orchard[i][j] fruits. Three gatherers start at the corners (0, 0), (0, n - 1) and (n - 1, 0). Each makes exactly n - 1 moves and must finish at (n - 1, n - 1).
- The one from (0, 0) moves from (i, j) to (i + 1, j + 1), (i + 1, j) or (i, j + 1).
- The one from (0, n - 1) moves to (i + 1, j - 1), (i + 1, j) or (i + 1, j + 1).
- The one from (n - 1, 0) moves to (i - 1, j + 1), (i, j + 1) or (i + 1, j + 1).
Nobody may leave the orchard. A gatherer takes all the fruit from every cell it visits, the starting cell included. When several gatherers visit the same cell, its fruit is counted only once.
Return the largest number of fruits the three can collect together.orchard=[[2, 0, 5], [1, 3, 4], [6, 7, 1]] → 28
The first gatherer walks the diagonal: 2 + 3 + 1. The second goes (0, 2) → (1, 2) → (2, 2) and collects 5 + 4. The third goes (2, 0) → (2, 1) → (2, 2) and collects 6 + 7. The corner (2, 2) is already empty. Total 6 + 9 + 13 = 28.
orchard=[[5, 0], [0, 5]] → 10
Each gatherer makes one step into the bottom-right corner; both 5s are collected, 10 in total.
orchard=[[2, 0, 5], [1, 3, 4], [6, 7, 1]]
28
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.