forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path6.cpp
More file actions
29 lines (24 loc) Β· 640 Bytes
/
6.cpp
File metadata and controls
29 lines (24 loc) Β· 640 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
#include <bits/stdc++.h>
using namespace std;
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
int d[100];
int n;
vector<int> arr;
int main(void) {
// μ μ Nμ μ
λ ₯λ°κΈ°
cin >> n;
// λͺ¨λ μλ μ 보 μ
λ ₯λ°κΈ°
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = arr[0];
d[1] = max(arr[0], arr[1]);
for (int i = 2; i < n; i++) {
d[i] = max(d[i - 1], d[i - 2] + arr[i]);
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
cout << d[n - 1] << '\n';
}