//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx2")
using namespace std;
#define F first
#define S second
#define pb push_back
#define sze size()
#define all(x) x.begin() , x.end()
#define wall__ cout << "--------------------------------------\n";
#define kids int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);
typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;
const ll N = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll INF = 1e9 + 10;
const ll lg = 18;
ll n, dp[N][lg][lg], pd[N], a[N];
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n; i >= 1; --i) {
for (int j = 0; j < lg; j++) dp[i][j][lg - 1] = INF;
for (int k = lg - 2; k >= 0; --k) {
int x = a[i] - k;
if (x != 0) dp[i][0][k] = INF;
else dp[i][0][k] = 0;
for (int j = 1; j < lg; j++) {
if (i + (1 << j) > n + 1) dp[i][j][k] = INF;
else dp[i][j][k] = min(dp[i][j - 1][k], dp[i][j - 1][k + 1] + 1) + min(dp[i + (1 << (j - 1))][j - 1][k], dp[i + (1 << (j - 1))][j - 1][k + 1] + 1);
}
}
}
pd[n + 1] = 0;
for (int i = n; i >= 1; --i) {
if (a[i] == 0) pd[i] = pd[i + 1];
else {
pd[i] = INF;
for (int j = 0; j < lg; j++) {
if ((1 << j) + i > n + 1) continue;
pd[i] = min(pd[i], dp[i][j][1] + 1 + pd[i + (1 << j)]);
}
}
}
if (pd[1] >= INF) cout << -1 << '\n';
else cout << pd[1] << '\n';
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {solve();}
return 0;
}
/*
*/
//shrek is love;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
2 ms |
1780 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1864 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1860 KB |
Output is correct |
13 |
Correct |
2 ms |
1796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1868 KB |
Output is correct |
2 |
Correct |
1 ms |
1748 KB |
Output is correct |
3 |
Correct |
1 ms |
1748 KB |
Output is correct |
4 |
Correct |
1 ms |
1748 KB |
Output is correct |
5 |
Correct |
1 ms |
1748 KB |
Output is correct |
6 |
Correct |
2 ms |
1780 KB |
Output is correct |
7 |
Correct |
1 ms |
1748 KB |
Output is correct |
8 |
Correct |
2 ms |
1748 KB |
Output is correct |
9 |
Correct |
1 ms |
1864 KB |
Output is correct |
10 |
Correct |
1 ms |
1748 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
2 ms |
1860 KB |
Output is correct |
13 |
Correct |
2 ms |
1796 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
3 ms |
2772 KB |
Output is correct |
20 |
Correct |
17 ms |
25820 KB |
Output is correct |
21 |
Correct |
17 ms |
25812 KB |
Output is correct |
22 |
Correct |
295 ms |
255728 KB |
Output is correct |
23 |
Correct |
213 ms |
255848 KB |
Output is correct |
24 |
Correct |
231 ms |
255636 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
1 ms |
584 KB |
Output is correct |
28 |
Correct |
2 ms |
2772 KB |
Output is correct |
29 |
Correct |
27 ms |
25812 KB |
Output is correct |
30 |
Correct |
26 ms |
25764 KB |
Output is correct |
31 |
Correct |
23 ms |
25844 KB |
Output is correct |
32 |
Correct |
26 ms |
25792 KB |
Output is correct |
33 |
Correct |
263 ms |
255624 KB |
Output is correct |
34 |
Correct |
239 ms |
255696 KB |
Output is correct |
35 |
Correct |
230 ms |
255696 KB |
Output is correct |
36 |
Correct |
217 ms |
255596 KB |
Output is correct |
37 |
Correct |
219 ms |
255692 KB |
Output is correct |
38 |
Correct |
219 ms |
255664 KB |
Output is correct |
39 |
Correct |
218 ms |
255636 KB |
Output is correct |
40 |
Correct |
218 ms |
255736 KB |
Output is correct |
41 |
Correct |
206 ms |
255684 KB |
Output is correct |
42 |
Correct |
211 ms |
255668 KB |
Output is correct |
43 |
Correct |
231 ms |
255680 KB |
Output is correct |
44 |
Correct |
278 ms |
255628 KB |
Output is correct |
45 |
Correct |
234 ms |
255592 KB |
Output is correct |
46 |
Correct |
269 ms |
255692 KB |
Output is correct |
47 |
Correct |
169 ms |
170532 KB |
Output is correct |
48 |
Correct |
170 ms |
198904 KB |
Output is correct |
49 |
Correct |
202 ms |
227312 KB |
Output is correct |
50 |
Correct |
221 ms |
255680 KB |
Output is correct |
51 |
Correct |
20 ms |
25816 KB |
Output is correct |
52 |
Correct |
217 ms |
255688 KB |
Output is correct |