atcoder#ABC175E. [ABC175E] Picking Goods
[ABC175E] Picking Goods
Score : points
Problem Statement
There are items placed on a grid of squares with rows and columns. Let denote the square at the -th row () and the -th column (). The -th item is at and has the value .
Takahashi will begin at , the start, and get to , the goal. When he is at , he can move to or (but cannot move to a non-existent square).
He can pick up items on the squares he visits, including the start and the goal, but at most three for each row. It is allowed to ignore the item on a square he visits.
Find the maximum possible sum of the values of items he picks up.
Constraints
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Output
Print the maximum possible sum of the values of items Takahashi picks up.
2 2 3
1 1 3
2 1 4
1 2 5
8
He has two ways to get to the goal:
- Visit , , and , in this order. In this case, the total value of the items he can pick up is .
- Visit , , and , in this order. In this case, the total value of the items he can pick up is .
Thus, the maximum possible sum of the values of items he picks up is .
2 5 5
1 1 3
2 4 20
1 2 1
1 3 4
1 4 2
29
We have four items in the -st row. The optimal choices are as follows:
- Visit , , , , and , in this order, and pick up all items except the one on . Then, the total value of the items he picks up will be .
4 5 10
2 5 12
1 5 12
2 3 15
1 2 20
1 1 28
2 4 26
3 2 27
4 5 21
3 5 10
1 3 10
142