#include <bits/stdc++.h>
#define int long long
// #define ll long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
using namespace std;
const int oo = 1e9 + 9;
const int MAX = 1e5 + 5, LOGMAX = 20, B = 800, MOD = 1e9 + 7;
int n;
int arr[MAX];
int dp[MAX][LOGMAX][LOGMAX];
int dp2[MAX];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> arr[i];
for(int i = 0; i < MAX; i++){
for(int j = 0; j < LOGMAX; j++){
for(int k = 0; k < LOGMAX; k++){
dp[i][j][k] = oo;
}
}
}
for(int i = 1; i <= n; i++){
dp[i][0][arr[i]] = 0;
dp[i][0][arr[i] - 1] = 1;
}
for(int i = n; i >= 1; i--){
for(int j = 1; i + (1 << j) - 1 <= n; j++){
for(int k = min(LOGMAX - 1, arr[i]); k >= 0; k--){
dp[i][j][k] = min({dp[i][j - 1][k] + dp[i + (1 << (j - 1))][j - 1][k],
dp[i][j - 1][k + 1] + dp[i + (1 << (j - 1))][j - 1][k + 1] + 1});
// cout << i << ' ' << j << ' ' << k << ": " << dp[i][j][k] << '\n';
}
}
}
for(int i = 1; i <= n; i++){
dp2[i] = oo;
for(int j = 0; i - (1 << j) >= 0; j++){
dp2[i] = min(dp2[i - (1 << j)] + dp[i - (1 << j) + 1][j][0], dp2[i]);
}
}
if(dp2[n] >= oo) dp2[n] = -1;
cout << dp2[n] << '\n';
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
314196 KB |
Output is correct |
2 |
Correct |
48 ms |
314196 KB |
Output is correct |
3 |
Correct |
48 ms |
314348 KB |
Output is correct |
4 |
Correct |
51 ms |
314164 KB |
Output is correct |
5 |
Correct |
51 ms |
314216 KB |
Output is correct |
6 |
Correct |
47 ms |
314196 KB |
Output is correct |
7 |
Correct |
58 ms |
314452 KB |
Output is correct |
8 |
Correct |
53 ms |
314196 KB |
Output is correct |
9 |
Correct |
49 ms |
314192 KB |
Output is correct |
10 |
Correct |
47 ms |
314192 KB |
Output is correct |
11 |
Correct |
53 ms |
314292 KB |
Output is correct |
12 |
Correct |
65 ms |
314196 KB |
Output is correct |
13 |
Correct |
53 ms |
314288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
314196 KB |
Output is correct |
2 |
Correct |
48 ms |
314196 KB |
Output is correct |
3 |
Correct |
48 ms |
314348 KB |
Output is correct |
4 |
Correct |
51 ms |
314164 KB |
Output is correct |
5 |
Correct |
51 ms |
314216 KB |
Output is correct |
6 |
Correct |
47 ms |
314196 KB |
Output is correct |
7 |
Correct |
58 ms |
314452 KB |
Output is correct |
8 |
Correct |
53 ms |
314196 KB |
Output is correct |
9 |
Correct |
49 ms |
314192 KB |
Output is correct |
10 |
Correct |
47 ms |
314192 KB |
Output is correct |
11 |
Correct |
53 ms |
314292 KB |
Output is correct |
12 |
Correct |
65 ms |
314196 KB |
Output is correct |
13 |
Correct |
53 ms |
314288 KB |
Output is correct |
14 |
Correct |
50 ms |
314192 KB |
Output is correct |
15 |
Correct |
52 ms |
314316 KB |
Output is correct |
16 |
Correct |
51 ms |
314176 KB |
Output is correct |
17 |
Correct |
55 ms |
314192 KB |
Output is correct |
18 |
Correct |
51 ms |
314196 KB |
Output is correct |
19 |
Correct |
53 ms |
314192 KB |
Output is correct |
20 |
Correct |
58 ms |
314452 KB |
Output is correct |
21 |
Correct |
64 ms |
314324 KB |
Output is correct |
22 |
Correct |
143 ms |
315360 KB |
Output is correct |
23 |
Correct |
178 ms |
315284 KB |
Output is correct |
24 |
Correct |
136 ms |
315212 KB |
Output is correct |
25 |
Correct |
48 ms |
314192 KB |
Output is correct |
26 |
Correct |
52 ms |
314192 KB |
Output is correct |
27 |
Correct |
62 ms |
314196 KB |
Output is correct |
28 |
Correct |
52 ms |
314344 KB |
Output is correct |
29 |
Correct |
57 ms |
314448 KB |
Output is correct |
30 |
Correct |
57 ms |
314452 KB |
Output is correct |
31 |
Correct |
57 ms |
314452 KB |
Output is correct |
32 |
Correct |
57 ms |
314472 KB |
Output is correct |
33 |
Correct |
120 ms |
315308 KB |
Output is correct |
34 |
Correct |
157 ms |
315064 KB |
Output is correct |
35 |
Correct |
141 ms |
315220 KB |
Output is correct |
36 |
Correct |
128 ms |
315216 KB |
Output is correct |
37 |
Correct |
162 ms |
315404 KB |
Output is correct |
38 |
Correct |
132 ms |
315220 KB |
Output is correct |
39 |
Correct |
120 ms |
315476 KB |
Output is correct |
40 |
Correct |
138 ms |
315224 KB |
Output is correct |
41 |
Correct |
126 ms |
315220 KB |
Output is correct |
42 |
Correct |
159 ms |
315192 KB |
Output is correct |
43 |
Correct |
135 ms |
315436 KB |
Output is correct |
44 |
Correct |
131 ms |
315216 KB |
Output is correct |
45 |
Correct |
133 ms |
315216 KB |
Output is correct |
46 |
Correct |
132 ms |
315240 KB |
Output is correct |
47 |
Correct |
106 ms |
314964 KB |
Output is correct |
48 |
Correct |
98 ms |
314964 KB |
Output is correct |
49 |
Correct |
123 ms |
315220 KB |
Output is correct |
50 |
Correct |
130 ms |
315192 KB |
Output is correct |
51 |
Correct |
56 ms |
314384 KB |
Output is correct |
52 |
Correct |
126 ms |
315244 KB |
Output is correct |