#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define vc vector
#define pb push_back
#define fr first
#define sc second
const int MAXN = 1 << 17 , lg = 19 , inf = 1e8;
int n , a[MAXN] , dp[MAXN] , pd[MAXN][lg][lg];
int main(){
cin >> n;
for(int i = 0 ; i < n ; i++) cin >> a[i];
for(int i = 0 ; i < n ; i++){
if(a[i] > lg){
cout << -1 << endl;
return 0;
}
}
for(int i = 0 ; i < n ; i++){
for(int d = 0 ; d < lg ; d++){
if(a[i] > d + 1 or a[i] < d) pd[i][0][d] = inf;
else pd[i][0][d] = a[i] - d;
}
}
for(int j = 1 ; j < lg ; j++){
for(int i = 0 ; i + (1 << j) <= n ; i++){
for(int d = 0 ; d < lg-1 ; d++){
pd[i][j][d] = min(pd[i][j-1][d] + pd[i+(1<<(j-1))][j-1][d] ,
pd[i][j-1][d+1] + pd[i+(1<<(j-1))][j-1][d+1] + 1);
pd[i][j][d] = min(pd[i][j][d] , inf);
}
}
}
for(int i = n-1 ; i >= 0 ; i--){
dp[i] = inf;
for(int j = 0 ; (1 << j) + i <= n ; j++){
dp[i] = min(dp[i] , pd[i][j][0] + dp[i+(1<<j)]);
}
}
if(dp[0] < inf) cout << dp[0] << endl;
else cout << -1 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1060 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1072 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1100 KB |
Output is correct |
4 |
Correct |
1 ms |
1060 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
2 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1072 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
1 ms |
1100 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
1612 KB |
Output is correct |
19 |
Correct |
2 ms |
1708 KB |
Output is correct |
20 |
Correct |
17 ms |
14500 KB |
Output is correct |
21 |
Correct |
17 ms |
14412 KB |
Output is correct |
22 |
Correct |
307 ms |
142628 KB |
Output is correct |
23 |
Correct |
25 ms |
964 KB |
Output is correct |
24 |
Correct |
288 ms |
142540 KB |
Output is correct |
25 |
Correct |
0 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
424 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
1708 KB |
Output is correct |
29 |
Correct |
22 ms |
14480 KB |
Output is correct |
30 |
Correct |
18 ms |
14492 KB |
Output is correct |
31 |
Correct |
17 ms |
14488 KB |
Output is correct |
32 |
Correct |
22 ms |
14484 KB |
Output is correct |
33 |
Correct |
287 ms |
142540 KB |
Output is correct |
34 |
Correct |
312 ms |
142616 KB |
Output is correct |
35 |
Correct |
306 ms |
142660 KB |
Output is correct |
36 |
Correct |
291 ms |
142532 KB |
Output is correct |
37 |
Correct |
316 ms |
142608 KB |
Output is correct |
38 |
Correct |
287 ms |
142500 KB |
Output is correct |
39 |
Correct |
292 ms |
142540 KB |
Output is correct |
40 |
Correct |
300 ms |
142608 KB |
Output is correct |
41 |
Correct |
292 ms |
142632 KB |
Output is correct |
42 |
Correct |
301 ms |
142724 KB |
Output is correct |
43 |
Correct |
294 ms |
142544 KB |
Output is correct |
44 |
Correct |
294 ms |
142660 KB |
Output is correct |
45 |
Correct |
294 ms |
142656 KB |
Output is correct |
46 |
Correct |
293 ms |
142484 KB |
Output is correct |
47 |
Correct |
195 ms |
95236 KB |
Output is correct |
48 |
Correct |
222 ms |
110864 KB |
Output is correct |
49 |
Correct |
254 ms |
126632 KB |
Output is correct |
50 |
Correct |
302 ms |
142452 KB |
Output is correct |
51 |
Correct |
19 ms |
14416 KB |
Output is correct |
52 |
Correct |
290 ms |
142540 KB |
Output is correct |