#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define int long long
const int64_t INF = 1 << 30;
const int N = 1e5 + 5;
const int LG = 17;
int dp[N][LG][LG], mn[N][LG], a[N], n, lg, DP[N];
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n;
FOR(i, 0, n) cin >> a[i];
for (int x = n - 1; x >= 0; x--) {
mn[x][0] = a[x];
for (int i = 1; (1 << i) + x <= n; i++)
mn[x][i] = min(mn[x][i - 1], mn[x + (1 << (i - 1))][i - 1]);
FOR(j, 0, LG) {
if (a[x] <= j + 1) dp[x][0][j] = (a[x] == j + 1);
else dp[x][0][j] = INF;
}
for (int i = 1; (1 << i) + x <= n; i++) {
FOR(j, 0, LG) {
dp[x][i][j] = dp[x][i - 1][j] + dp[x + (1 << (i - 1))][i - 1][j];
if (mn[x][i] > j)
dp[x][i][j] = min(dp[x][i][j], dp[x][i - 1][j + 1] + dp[x + (1 << (i - 1))][i - 1][j + 1] + 1);
}
}
}
fill(DP, DP + N, INF);
DP[n] = 0;
for (int x = n - 1; x >= 0; x--) {
for (int i = 0; (1 << i) + x <= n; i++) {
DP[x] = min(DP[x], dp[x][i][0] + DP[x + (1 << i)]);
}
}
cout << (DP[0] == INF ? -1 : DP[0]) << '\n';
}
//10:39:29
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2516 KB |
Output is correct |
2 |
Correct |
2 ms |
2516 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2504 KB |
Output is correct |
5 |
Correct |
1 ms |
2500 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2508 KB |
Output is correct |
8 |
Correct |
1 ms |
2516 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2516 KB |
Output is correct |
2 |
Correct |
2 ms |
2516 KB |
Output is correct |
3 |
Correct |
1 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
2504 KB |
Output is correct |
5 |
Correct |
1 ms |
2500 KB |
Output is correct |
6 |
Correct |
1 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2508 KB |
Output is correct |
8 |
Correct |
1 ms |
2516 KB |
Output is correct |
9 |
Correct |
1 ms |
2516 KB |
Output is correct |
10 |
Correct |
1 ms |
2516 KB |
Output is correct |
11 |
Correct |
1 ms |
2512 KB |
Output is correct |
12 |
Correct |
2 ms |
2560 KB |
Output is correct |
13 |
Correct |
1 ms |
2516 KB |
Output is correct |
14 |
Correct |
1 ms |
1108 KB |
Output is correct |
15 |
Correct |
1 ms |
1108 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
17 |
Correct |
1 ms |
1348 KB |
Output is correct |
18 |
Correct |
2 ms |
3408 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
16 ms |
25172 KB |
Output is correct |
21 |
Correct |
16 ms |
25172 KB |
Output is correct |
22 |
Correct |
207 ms |
241656 KB |
Output is correct |
23 |
Correct |
184 ms |
241768 KB |
Output is correct |
24 |
Correct |
178 ms |
241576 KB |
Output is correct |
25 |
Correct |
1 ms |
1108 KB |
Output is correct |
26 |
Correct |
1 ms |
1236 KB |
Output is correct |
27 |
Correct |
1 ms |
1348 KB |
Output is correct |
28 |
Correct |
2 ms |
3412 KB |
Output is correct |
29 |
Correct |
16 ms |
25076 KB |
Output is correct |
30 |
Correct |
20 ms |
25172 KB |
Output is correct |
31 |
Correct |
16 ms |
25172 KB |
Output is correct |
32 |
Correct |
16 ms |
25172 KB |
Output is correct |
33 |
Correct |
214 ms |
241484 KB |
Output is correct |
34 |
Correct |
192 ms |
241756 KB |
Output is correct |
35 |
Correct |
204 ms |
241592 KB |
Output is correct |
36 |
Correct |
181 ms |
241596 KB |
Output is correct |
37 |
Correct |
182 ms |
241616 KB |
Output is correct |
38 |
Correct |
179 ms |
241508 KB |
Output is correct |
39 |
Correct |
175 ms |
241512 KB |
Output is correct |
40 |
Correct |
183 ms |
241648 KB |
Output is correct |
41 |
Correct |
182 ms |
241560 KB |
Output is correct |
42 |
Correct |
199 ms |
241668 KB |
Output is correct |
43 |
Correct |
174 ms |
241644 KB |
Output is correct |
44 |
Correct |
188 ms |
241684 KB |
Output is correct |
45 |
Correct |
176 ms |
241584 KB |
Output is correct |
46 |
Correct |
196 ms |
241516 KB |
Output is correct |
47 |
Correct |
111 ms |
161328 KB |
Output is correct |
48 |
Correct |
131 ms |
188108 KB |
Output is correct |
49 |
Correct |
156 ms |
214796 KB |
Output is correct |
50 |
Correct |
186 ms |
241604 KB |
Output is correct |
51 |
Correct |
16 ms |
25176 KB |
Output is correct |
52 |
Correct |
180 ms |
241680 KB |
Output is correct |