-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathE.java
116 lines (100 loc) · 3.29 KB
/
E.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.util.ArrayDeque;
import java.util.Scanner;
public class E {
static int inf = 1000000;
static int dp[][];
static int a[];
static int DP(int i, int j) {
if (j > i) return inf;
else {
int res;
int cost = a[i];
if (j <= 0) {
if (i >= 1) {
if (cost <= 100) {
int dif = Math.min(DP(i - 1, j + 1), DP(i - 1, j) + cost);
res = dif;
} else {
return DP(i - 1, j + 1);
}
} else return 0;
} else {
if (dp[i][j] != -1) return dp[i][j];
if (cost > 100) {
int dif = Math.min(DP(i - 1, j + 1), DP(i - 1, j - 1) + cost);
res = dif;
} else {
int dif = Math.min(DP(i - 1, j + 1), DP(i - 1, j) + cost);
res = dif;
}
}
dp[i][j] = res;
return res;
}
}
static void GOODOLDDAYS(ArrayDeque<Integer> used, int i, int j) {
if (j < i) {
int cost = a[i];
if (j <= 0) {
if (i >= 1) {
if (cost > 100) {
used.add(i);
GOODOLDDAYS(used, i - 1, j + 1);
} else {
boolean addi = (DP(i, j) == DP(i - 1, j + 1));
if (addi) {
used.add(i);
GOODOLDDAYS(used, i - 1, j + 1);
} else GOODOLDDAYS(used, i - 1, j);
}
}
} else {
if (cost <= 100) {
boolean addi = (DP(i - 1, j + 1) == DP(i, j));
if (addi) {
used.add(i);
GOODOLDDAYS(used, i - 1, j + 1);
} else {
GOODOLDDAYS(used, i - 1, j);
}
} else {
boolean addi = (DP(i - 1, j + 1) == DP(i, j));
if (addi) {
used.add(i);
GOODOLDDAYS(used, i - 1, j + 1);
} else {
GOODOLDDAYS(used, i - 1, j - 1);
}
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int k1 = 0;
int k2 = 0;
a = new int[n + 1];
for (int i = 1; i <= n; i++) a[i] = scanner.nextInt();
dp = new int[n + 1][n + 2];
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n + 1; j++)
dp[i][j] = -1;
}
int ans = inf;
for (int i = 0; i <= n; i++) {
if (ans >= DP(n, i)) {
ans = DP(n, i);
k1 = i;
}
}
System.out.println(ans);
ArrayDeque<Integer> used = new ArrayDeque<>();
GOODOLDDAYS(used, n, k1);
k2 = used.size();
System.out.println(k1 + " " + k2);
while (!used.isEmpty()) {
System.out.print(used.removeLast() + "\n");
}
}
}