#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
const int L = 22;
const int INF = 0x3f3f3f3f;
int n;
int a[N];
int dp[N][L];
int ndp[N][L];
int mn[N][L];
int kek[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
for (int i = 0; i < n; ++i) {
if (a[i] >= L) {
cout << "-1\n";
return 0;
}
}
memset(dp, 0x3f, sizeof dp);
for (int i = 0; i < n; ++i) {
dp[i][a[i]] = 0;
if (a[i] > 0) {
dp[i][a[i] - 1] = 1;
}
}
for (int i = 0; i < n; ++i) {
mn[i][0] = dp[i][0];
}
for (int t = 1; (1 << t) <= n; ++t) {
memset(ndp, 0x3f, sizeof ndp);
for (int i = 0; i + (1 << t) <= n; ++i) {
for (int j = 0; j < L; ++j) {
ndp[i][j] = min(ndp[i][j], dp[i][j] + dp[i + (1 << (t - 1))][j]);
if (j + 1 < L) {
ndp[i][j] = min(ndp[i][j], dp[i][j + 1] + dp[i + (1 << (t - 1))][j + 1] + 1);
}
}
}
memcpy(dp, ndp, sizeof dp);
for (int i = 0; i + (1 << t) <= n; ++i) {
mn[i][t] = dp[i][0];
}
}
memset(kek, 0x3f, sizeof kek);
kek[n] = 0;
for (int i = n - 1; i >= 0; --i) {
for (int t = 0; i + (1 << t) <= n; ++t) {
kek[i] = min(kek[i], kek[i + (1 << t)] + mn[i][t]);
}
}
if (kek[0] == INF) {
cout << "-1\n";
return 0;
}
cout << kek[0] << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
17868 KB |
Output is correct |
2 |
Correct |
23 ms |
17868 KB |
Output is correct |
3 |
Correct |
23 ms |
17860 KB |
Output is correct |
4 |
Correct |
24 ms |
17860 KB |
Output is correct |
5 |
Correct |
23 ms |
17864 KB |
Output is correct |
6 |
Correct |
23 ms |
17992 KB |
Output is correct |
7 |
Correct |
23 ms |
17876 KB |
Output is correct |
8 |
Correct |
25 ms |
17872 KB |
Output is correct |
9 |
Correct |
26 ms |
17988 KB |
Output is correct |
10 |
Correct |
28 ms |
17900 KB |
Output is correct |
11 |
Correct |
23 ms |
17896 KB |
Output is correct |
12 |
Correct |
27 ms |
17976 KB |
Output is correct |
13 |
Correct |
22 ms |
17860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
17868 KB |
Output is correct |
2 |
Correct |
23 ms |
17868 KB |
Output is correct |
3 |
Correct |
23 ms |
17860 KB |
Output is correct |
4 |
Correct |
24 ms |
17860 KB |
Output is correct |
5 |
Correct |
23 ms |
17864 KB |
Output is correct |
6 |
Correct |
23 ms |
17992 KB |
Output is correct |
7 |
Correct |
23 ms |
17876 KB |
Output is correct |
8 |
Correct |
25 ms |
17872 KB |
Output is correct |
9 |
Correct |
26 ms |
17988 KB |
Output is correct |
10 |
Correct |
28 ms |
17900 KB |
Output is correct |
11 |
Correct |
23 ms |
17896 KB |
Output is correct |
12 |
Correct |
27 ms |
17976 KB |
Output is correct |
13 |
Correct |
22 ms |
17860 KB |
Output is correct |
14 |
Correct |
12 ms |
17876 KB |
Output is correct |
15 |
Correct |
12 ms |
17876 KB |
Output is correct |
16 |
Correct |
18 ms |
17876 KB |
Output is correct |
17 |
Correct |
18 ms |
17876 KB |
Output is correct |
18 |
Correct |
24 ms |
18032 KB |
Output is correct |
19 |
Correct |
27 ms |
18028 KB |
Output is correct |
20 |
Correct |
38 ms |
18764 KB |
Output is correct |
21 |
Correct |
36 ms |
18764 KB |
Output is correct |
22 |
Correct |
110 ms |
27160 KB |
Output is correct |
23 |
Correct |
6 ms |
980 KB |
Output is correct |
24 |
Correct |
114 ms |
27104 KB |
Output is correct |
25 |
Correct |
12 ms |
17876 KB |
Output is correct |
26 |
Correct |
20 ms |
17816 KB |
Output is correct |
27 |
Correct |
19 ms |
17952 KB |
Output is correct |
28 |
Correct |
24 ms |
17896 KB |
Output is correct |
29 |
Correct |
36 ms |
18840 KB |
Output is correct |
30 |
Correct |
35 ms |
18744 KB |
Output is correct |
31 |
Correct |
35 ms |
18764 KB |
Output is correct |
32 |
Correct |
40 ms |
18848 KB |
Output is correct |
33 |
Correct |
109 ms |
27104 KB |
Output is correct |
34 |
Correct |
106 ms |
27212 KB |
Output is correct |
35 |
Correct |
107 ms |
27192 KB |
Output is correct |
36 |
Correct |
111 ms |
27112 KB |
Output is correct |
37 |
Correct |
112 ms |
27316 KB |
Output is correct |
38 |
Correct |
107 ms |
27116 KB |
Output is correct |
39 |
Correct |
109 ms |
27104 KB |
Output is correct |
40 |
Correct |
108 ms |
27056 KB |
Output is correct |
41 |
Correct |
112 ms |
27116 KB |
Output is correct |
42 |
Correct |
107 ms |
27208 KB |
Output is correct |
43 |
Correct |
111 ms |
27212 KB |
Output is correct |
44 |
Correct |
107 ms |
26996 KB |
Output is correct |
45 |
Correct |
107 ms |
26992 KB |
Output is correct |
46 |
Correct |
106 ms |
27216 KB |
Output is correct |
47 |
Correct |
98 ms |
24020 KB |
Output is correct |
48 |
Correct |
95 ms |
25072 KB |
Output is correct |
49 |
Correct |
98 ms |
25976 KB |
Output is correct |
50 |
Correct |
110 ms |
27120 KB |
Output is correct |
51 |
Correct |
35 ms |
18796 KB |
Output is correct |
52 |
Correct |
109 ms |
27108 KB |
Output is correct |