#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
map<pair<pair<int,int>,int>,int> mp;
int lg[N], n, a[N];
int calc(int l, int r, int dec=0) {
if(mp.find({{l, r}, dec}) != mp.end()) {
return mp[{{l,r},dec}];
}
if(lg[r-l+1] == 0)return -1;
if(l == r) {
if(a[l]-dec > 1)return -1;
return a[l]-dec;
}
int mn = 1e9;
for(int i = l;i<=r;i++) {
mn=min(mn, a[i]-dec);
}
mn = (mn>0);
int mid = (l+r)/2;
long long L = calc(l, mid, dec+mn);
long long R = calc(mid+1, r, dec+mn);
if(L == -1 or R==-1)return -1;
return mp[{{l,r},dec}]=L+R+mn;
}
int main () {
{
int cntlg=1;
for(int i = 1;i<N;i*=2) {
lg[i]=cntlg++;
}
}
cin >> n;
for(int i = 0;i<n;i++) {
cin >> a[i];
}
long long dp[n+1];
dp[0] = 0;
for(int i= 1;i<=n;i++) {
dp[i] = 1e18;
for(int j = 1;j<=i;j*=2) {
long long val = calc(i-j, i-1);
if(val == -1)continue;
dp[i] = min(dp[i], dp[i-j] + val);
}
}
if(dp[n] == 1e18)cout << "-1\n";
else cout << dp[n] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
860 KB |
Output is correct |
2 |
Correct |
4 ms |
992 KB |
Output is correct |
3 |
Correct |
14 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
480 KB |
Output is correct |
5 |
Correct |
9 ms |
628 KB |
Output is correct |
6 |
Correct |
10 ms |
604 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
12 ms |
804 KB |
Output is correct |
9 |
Correct |
9 ms |
604 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
10 ms |
820 KB |
Output is correct |
12 |
Correct |
7 ms |
632 KB |
Output is correct |
13 |
Correct |
10 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
860 KB |
Output is correct |
2 |
Correct |
4 ms |
992 KB |
Output is correct |
3 |
Correct |
14 ms |
348 KB |
Output is correct |
4 |
Correct |
11 ms |
480 KB |
Output is correct |
5 |
Correct |
9 ms |
628 KB |
Output is correct |
6 |
Correct |
10 ms |
604 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
12 ms |
804 KB |
Output is correct |
9 |
Correct |
9 ms |
604 KB |
Output is correct |
10 |
Correct |
10 ms |
604 KB |
Output is correct |
11 |
Correct |
10 ms |
820 KB |
Output is correct |
12 |
Correct |
7 ms |
632 KB |
Output is correct |
13 |
Correct |
10 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
18 ms |
1672 KB |
Output is correct |
19 |
Correct |
32 ms |
348 KB |
Output is correct |
20 |
Execution timed out |
2037 ms |
652 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |