// In the name of god
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
const ll maxn = 1e5 + 5;
const ll inf = 1e18;
const ll lg = 20;
ll n, a[maxn], dp[maxn][lg + 1][lg + 1][2], pw2[lg + 1], ANS[maxn];
int main(){
pw2[0] = 1;
for(ll i = 1; i < lg; i ++)pw2[i] = (pw2[i - 1] * 2ll);
cin >> n;
for(ll i = 0; i < n; i ++){
cin >> a[i];
for(ll j = 0; j < lg; j ++){
for(ll k2 = 0; k2 < 2; k2 ++){
if(a[i] < j)dp[i][0][j][k2] = -1;
else if(a[i] == j)dp[i][0][j][k2] = 0;
else if(a[i] == j + 1)dp[i][0][j][k2] = 1;
else dp[i][0][j][k2] = -1;
}
}
ll d;
ANS[i] = inf;
for(ll j = 1; pw2[j] < lg; j ++){
for(ll k = 0; k < lg; k ++){
for(ll k2 = 0; k2 < 2; k2 ++){
if(pw2[j] > (i + 1ll)){
dp[i][j][k][k2] = -1;
continue;
}
d = inf;
if((k < (lg - 1ll)) &&(dp[i][j - 1][k + 1][1] != -1) && (dp[i - pw2[j - 1]][j - 1][k + 1][1] != -1))
d = min(d, dp[i - pw2[j - 1]][j - 1][k + 1][1] + dp[i][j - 1][k + 1][1] + 1ll);
if(k2 == 0){
if((dp[i][j - 1][k][0] != -1) && (dp[i - pw2[j - 1]][j - 1][k][0] != -1))
d = min(d, dp[i - pw2[j - 1]][j - 1][k][0] + dp[i][j - 1][k][0]);
}
else{
if((dp[i][j - 1][k][1] != -1) && (dp[i - pw2[j - 1]][j - 1][k][1] != -1))
d = min(d, dp[i - pw2[j - 1]][j - 1][k][1] + dp[i][j - 1][k][1]);
}
if(d == inf)d = -1;
dp[i][j][k][k2] = d;
}
}
if(dp[i][j][0][0] != -1){
if(pw2[j] == i + 1ll)ANS[i] = min(ANS[i], dp[i][j][0][0]);
else if(ANS[i - pw2[j]] != -1)ANS[i] = min(ANS[i], dp[i][j][0][0] + ANS[i - pw2[j]]);
}
}
if(ANS[i] == inf)ANS[i] = -1;
}
cout << ANS[n - 1] << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
3660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |