-
-
Save ArthurLoboLobo/4e433e1db10c75e6a1ae582af67dfe73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
const int maxn = 1e5+10; | |
int n, a[maxn]; | |
int main() { | |
// freopen("in.in","r",stdin); | |
cin >> n; | |
for(int i = 1; i <= n; i++) { | |
cin >> a[i]; | |
} | |
int l = 1; | |
int r = n; | |
long long ans = 0; | |
while(l+1 < r) { | |
if(a[l] <= 0) { | |
ans++; | |
l++; | |
} | |
else if(a[r] <= 0) { | |
ans++; | |
r--; | |
} | |
else { | |
int mn = min(a[l],a[r]); | |
a[l]-=mn; | |
a[r]-=mn; | |
ans+= mn; | |
} | |
} | |
if(l+1 == r) { | |
int mn = min(a[l],a[r]); | |
a[l]-=mn; | |
a[r]-=mn; | |
ans+= mn; | |
if(a[l] == 0 && a[r] == 0) { | |
cout << ans << endl; | |
return 0; | |
} | |
else if(a[r] == 0) { | |
ans++; | |
r--; | |
} | |
else if(a[l] == 0) { | |
ans++; | |
l++; | |
} | |
} | |
if(a[l]%2 == 1) cout << -1 << endl; | |
else { | |
ans+= a[l]/2; | |
cout << ans << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment