#CT00046. Đường đi tối ưu (Optimal Path)
Đường đi tối ưu (Optimal Path)
Optimal Path
Version: Phuoc Hung OJ Extended
Problem Statement
You are given an by integer grid. Cell contains the integer .
A path starts at and ends at . From a cell, you may move only one cell to the right or one cell downward.
The value of a path is the sum of all cell values on it. Determine:
- The maximum possible path value.
- The number of paths attaining exactly that maximum, modulo .
Input
- The first line contains two positive integers .
- The next lines each contain integers; the -th number on row is .
Output
Print two integers separated by one space: the maximum path value and the number of optimal paths modulo .
Subtasks
- Subtask 1 (30%): , .
- Subtask 2 (30%): , .
- Subtask 3 (40%): , .
Examples
Example 1
Input
3 3
1 2 3
4 5 6
7 8 9
Output
29 1
Explanation
The optimal path visits values , with sum . No other path reaches this sum, so the number of optimal paths is .
Example 2
Input
2 2
1 1
1 1
Output
3 2
Explanation
There are two paths: right then down, or down then right. Both visit three cells of value , so both have sum .
Example 3
Input
2 3
1 5 1
1 1 5
Output
12 2
Explanation
There are three paths from to . Exactly two of them attain the maximum sum , so the answer is 12 2.
Liên quan
Trong các cuộc thi sau: