Picking plots on a terraced field
Statement
A terraced field of N levels has been cut into a hillside. The top is level 1, and going down the levels are numbered 2, 3 and on. Level i holds i plots, and the plots on each level are numbered 1, 2 and on from the left. Each plot has this year's yield written on it.
The farmer starts harvesting at the plot on level 1 and picks plots while going down one level at a time. The ladders between the plots lie at a slant, so from plot j on level i the farmer can only go down to plot j on level i+1 or plot j+1 on level i+1. The farmer goes down as far as level N, picking one plot on each level, and harvests the yield of every plot picked.
So, given the yield of each plot, write a program that finds the largest total yield the farmer can harvest.
For example, suppose N=5 and the yield of each plot is as in the table below. An empty cell is a place where that level has no plot.
| Level | Plot 1 | Plot 2 | Plot 3 | Plot 4 | Plot 5 |
|---|---|---|---|---|---|
| Level 1 | 18 | ||||
| Level 2 | 23 | 6 | |||
| Level 3 | 43 | 18 | 45 | ||
| Level 4 | 16 | 64 | 20 | 26 | |
| Level 5 | 42 | 39 | 32 | 84 | 77 |
Setting out from plot 1 on level 1 and picking plot 1 on level 2, plot 1 on level 3, plot 2 on level 4 and plot 2 on level 5 harvests the yields below.
| Level | Level 1 | Level 2 | Level 3 | Level 4 | Level 5 |
|---|---|---|---|---|---|
| Plot picked | Plot 1 | Plot 1 | Plot 1 | Plot 2 | Plot 2 |
| Yield | 18 | 23 | 43 | 64 | 39 |
| Total so far | 18 | 41 | 84 | 148 | 187 |
The plot with the largest yield on level 5 is plot 4, which holds 84. The plots on the way down to it hold small yields, though, so the total stops at 179. There is no path that makes a larger total, so the answer in this case is 187.
Writing on each plot the largest total that can be harvested on the way down to it lets the plot be filled in by looking only at the two plots on the level just above that can come down to it. Once the bottom level is filled in, picking the largest value on that level gives the answer.
Input and Output Examples
Example 1
Input
5 18 23 6 43 18 45 16 64 20 26 42 39 32 84 77
Output
187
Example 2
Input
3 75 39 21 39 55 27
Output
169
Example 3
Input
1 23
Output
23
Input
First the number of levels of the terraced field N is given as a natural number. (N is a natural number from 1 to 500.)
Then N lines follow, giving the yields of each level from level 1 in order. Line i holds the yields of the i plots on level i, separated by spaces, starting from the leftmost plot. Each yield is an integer from 0 to 9,999.
Output
Print the largest total yield the farmer can harvest on one line. Only one number is printed, so no space is left at the end of the line.
Requirements
- From plot j on level i you can only go down to plot j or plot j+1 on level i+1
- Set out on level 1 and go down as far as level N, picking one plot on each level
- If N is 1 there is only one plot, so its yield is the answer
- Some plots have a yield of 0, so picking the larger side on every level gives the wrong answer
- Filling in the largest total up to each plot from the top downward gets the answer in a single sweep
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 23
23
3 75 39 21 39 55 27
169
5 18 23 6 43 18 45 16 64 20 26 42 39 32 84 77
187
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.
Log in and you can see the code once you solve this problem.