forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path2.cpp
More file actions
34 lines (31 loc) Β· 963 Bytes
/
2.cpp
File metadata and controls
34 lines (31 loc) Β· 963 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int n;
int dp[500][500]; // λ€μ΄λλ―Ή νλ‘κ·Έλλ°μ μν DP ν
μ΄λΈ μ΄κΈ°ν
int main(void) {
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i + 1; j++) {
cin >> dp[i][j];
}
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°μΌλ‘ 2λ²μ§Έ μ€λΆν° λ΄λ €κ°λ©΄μ νμΈ
for (int i = 1; i < n; i++) {
for (int j = 0; j <= i; j++) {
int upLeft, up;
// μΌμͺ½ μμμ λ΄λ €μ€λ κ²½μ°
if (j == 0) upLeft = 0;
else upLeft = dp[i - 1][j - 1];
// λ°λ‘ μμμ λ΄λ €μ€λ κ²½μ°
if (j == i) up = 0;
else up = dp[i - 1][j];
// μ΅λ ν©μ μ μ₯
dp[i][j] = dp[i][j] + max(upLeft, up);
}
}
int result = 0;
for (int i = 0; i < n; i++) {
result = max(result, dp[n - 1][i]);
}
cout << result << '\n';
}