#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define bug(x) cout << #x << " : " << x << '\n'
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int maxlg = 18;
const int mod = 1e9 + 7;
const int inf = 1e9 + 10;
int a[maxn];
int Dp[maxlg][maxlg][maxn];
int dp[maxn];
int main(){
ios;
int n;
cin >> n;
for(int i = 0; i < n; i ++){
cin >> a[i];
for(int j = 0; j < maxlg; j ++){
int &r = Dp[0][j][i];
int x = a[i] - j;
if(x < 0 or x > 1)
r = inf;
else
r = x;
}
}
for(int i = 1; i < maxlg; i ++){
for(int j = 0; j < maxlg; j ++){
for(int ind = 0; ind + (1 << i) <= n; ind ++){
int &r = Dp[i][j][ind];
r = min(inf, Dp[i - 1][j][ind] + Dp[i - 1][j][ind + (1 << (i - 1))]);
if(j < maxlg - 1){
r = min(r, 1 + Dp[i - 1][j + 1][ind] + Dp[i - 1][j + 1][ind + (1 << (i - 1))]);
}
}
}
}
for(int i = n - 1; ~i; i --){
if(!a[i]){
dp[i] = dp[i + 1];
continue;
}
dp[i] = inf;
for(int j = 0; i + (1 << j) <= n; j ++){
dp[i] = min(dp[i], Dp[j][0][i] + dp[i + (1 << j)]);
}
}
if(dp[0] >= inf)
dp[0] = -1;
cout << dp[0] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1484 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1492 KB |
Output is correct |
2 |
Correct |
1 ms |
1492 KB |
Output is correct |
3 |
Correct |
1 ms |
1492 KB |
Output is correct |
4 |
Correct |
1 ms |
1492 KB |
Output is correct |
5 |
Correct |
1 ms |
1492 KB |
Output is correct |
6 |
Correct |
1 ms |
1492 KB |
Output is correct |
7 |
Correct |
1 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1492 KB |
Output is correct |
9 |
Correct |
1 ms |
1492 KB |
Output is correct |
10 |
Correct |
1 ms |
1484 KB |
Output is correct |
11 |
Correct |
1 ms |
1492 KB |
Output is correct |
12 |
Correct |
1 ms |
1484 KB |
Output is correct |
13 |
Correct |
1 ms |
1492 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
0 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
980 KB |
Output is correct |
17 |
Correct |
1 ms |
980 KB |
Output is correct |
18 |
Correct |
1 ms |
1748 KB |
Output is correct |
19 |
Correct |
1 ms |
1736 KB |
Output is correct |
20 |
Correct |
9 ms |
10324 KB |
Output is correct |
21 |
Correct |
8 ms |
10328 KB |
Output is correct |
22 |
Correct |
91 ms |
112468 KB |
Output is correct |
23 |
Correct |
93 ms |
112496 KB |
Output is correct |
24 |
Correct |
91 ms |
112368 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
1 ms |
980 KB |
Output is correct |
27 |
Correct |
1 ms |
968 KB |
Output is correct |
28 |
Correct |
1 ms |
1748 KB |
Output is correct |
29 |
Correct |
8 ms |
10324 KB |
Output is correct |
30 |
Correct |
8 ms |
10276 KB |
Output is correct |
31 |
Correct |
8 ms |
10272 KB |
Output is correct |
32 |
Correct |
8 ms |
10364 KB |
Output is correct |
33 |
Correct |
90 ms |
112308 KB |
Output is correct |
34 |
Correct |
94 ms |
112528 KB |
Output is correct |
35 |
Correct |
92 ms |
112480 KB |
Output is correct |
36 |
Correct |
108 ms |
112396 KB |
Output is correct |
37 |
Correct |
91 ms |
112432 KB |
Output is correct |
38 |
Correct |
96 ms |
112316 KB |
Output is correct |
39 |
Correct |
91 ms |
112392 KB |
Output is correct |
40 |
Correct |
92 ms |
112460 KB |
Output is correct |
41 |
Correct |
91 ms |
112316 KB |
Output is correct |
42 |
Correct |
98 ms |
112496 KB |
Output is correct |
43 |
Correct |
99 ms |
112384 KB |
Output is correct |
44 |
Correct |
91 ms |
112420 KB |
Output is correct |
45 |
Correct |
92 ms |
112332 KB |
Output is correct |
46 |
Correct |
92 ms |
112480 KB |
Output is correct |
47 |
Correct |
59 ms |
72948 KB |
Output is correct |
48 |
Correct |
71 ms |
86336 KB |
Output is correct |
49 |
Correct |
84 ms |
99812 KB |
Output is correct |
50 |
Correct |
95 ms |
112332 KB |
Output is correct |
51 |
Correct |
8 ms |
10380 KB |
Output is correct |
52 |
Correct |
91 ms |
112428 KB |
Output is correct |