Buy one get one dynamic planning questions, let me happily AC twice! The only difference is the size of the array.
Description
The cows don't use actual bowling ballwhen they go bowling. they each take a number (in the range 0 .. 99), though, and line up in a standard bowling-pin-like triangle like this:
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. the cow's score is the sum of the numbers of the cows visited along the way. the cow with the highest score wins that frame.
Given a triangle with N (1 <= n <= 350) rows, determine the highest possible sum achievable.
Input
Line 1: A single integer, n
Lines 2. n + 1: line I + 1 contains I space-separated integers that represent row I of the triangle.
Output
Line 1: the largest sum achievable using the traversal rules
Sample Input
573 88 1 02 7 4 44 5 2 6 5
Sample output
30
Hint
Explanation of the sample:
7
*
3 8
*
8 1 0
*
2 7 4 4
*
4 5 2 6 5
The highest score is achievable by traversing the cows as shown above.
# Include <iostream> <br/> # include <cmath> <br/> using namespace STD; <br/> const int max = 350 + 10; <br/> int main () <br/> {<br/> int T [Max] [Max]; <br/> int DP [Max] [Max]; <br/> memset (DP, 0, sizeof (DP); <br/> int N; <br/> CIN> N; <br/> for (INT I = 0; I <n; ++ I) <br/> for (Int J = 0; j <I + 1; ++ J) <br/>{< br/> CIN> T [I] [J]; <br/> DP [I] [J] = T [I] [J]; <br/>}< br/> for (INT I = N-2; I >= 0; -- I) <br/>{< br/> Int J = I + 1; <br/> for (INT x = 0; x <j; ++ X) <br/> DP [I] [x] = max (T [I] [x] + dp [J] [X], T [I] [x] + dp [J] [x + 1]); <br/>}< br/> cout <DP [0] [0] <Endl; <br/> return 0; <br/>}