This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |