-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPE_Plus_018.cpp
More file actions
51 lines (48 loc) · 1.3 KB
/
PE_Plus_018.cpp
File metadata and controls
51 lines (48 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int solve(int[][16], int);
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int t = 0;
cin >> t;
int n = 0;
for(int i = 0; i < t; i++){
cin >> n;
int stairs [n][16];
for(int i = 0; i < n; i++)
for(int j = 0; j < 16; j++)
stairs[i][j] = 0;
for(int i = 0; i < n; i++){
for(int j = 1; j <= i+1; j++)
cin >> stairs[i][j];
}
cout << solve(stairs, n) << endl;
}
return 0;
}
int solve(int stairs[][16], int n){
int solution[n+1][n+1];
for(int i = 0; i < n+1; i++)
for(int j = 0; j < n+1; j++)
solution[i][j] = 0;
solution[0][1] = stairs[0][1];
int left, right;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= i+1; j++){
left = solution[i-1][j-1];
right = solution[i-1][j];
if(left > right)
solution[i][j] = stairs[i][j] + left;
else
solution[i][j] = stairs[i][j] + right;
}
int best = 0;
for(int i = 0; i < n+1; i++)
if(best < solution[n-1][i])
best = solution[n-1][i];
return best;
}