#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll big = 1e18;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
ll n;
cin >> n;
vector<ll> a(n);
for(ll &i : a) cin >> i;
vector<ll> l(n,big),r(n,big);
ll cur = 0;
vector<ll> b = a;
for(ll i = 0;i<n;i++){
if (b[i]==1){
if (i+1==n) break;
if (b[i+1]==1){
b[i] = -1;
b[i+1] = -1;
cur++;
}else{
if (i+2==n||b[i+2]!=-1) break;
b[i] = -1;
b[i+2] = 1;
}
}
l[i] = cur;
}
b = a;
reverse(b.begin(),b.end());
cur = 0;
for(ll i = 0;i<n;i++){
if (b[i]==-1){
if (i+1==n) break;
if (b[i+1]==-1){
b[i] = 1;
b[i+1] = 1;
cur++;
}else{
if (i+2==n||b[i+2]!=1) break;
b[i] = 1;
b[i+2] = -1;
}
}
r[i] = cur;
}
ll ans = min(l.back(),r.back());
for(ll i = 0;i<n-1;i++){
ans = min(ans,l[i]+r[n-2-i]);
}
cout << (ans>=big?-1:ans) << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |