forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6.java
30 lines (23 loc) Β· 820 Bytes
/
6.java
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
import java.util.*;
public class Main {
// μμ κ³μ°λ κ²°κ³Όλ₯Ό μ μ₯νκΈ° μν DP ν
μ΄λΈ μ΄κΈ°ν
public static int[] d = new int[100];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// μ μ Nμ μ
λ ₯λ°κΈ°
int n = sc.nextInt();
// λͺ¨λ μλ μ 보 μ
λ ₯λ°κΈ°
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// λ€μ΄λλ―Ή νλ‘κ·Έλλ°(Dynamic Programming) μ§ν(보ν
μ
)
d[0] = arr[0];
d[1] = Math.max(arr[0], arr[1]);
for (int i = 2; i < n; i++) {
d[i] = Math.max(d[i - 1], d[i - 2] + arr[i]);
}
// κ³μ°λ κ²°κ³Ό μΆλ ₯
System.out.println(d[n - 1]);
}
}