fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define FAST ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  5. #define REP(i,a,n) for (int i=a;i<n;i++)
  6. #define ALL(x) (x).begin(),(x).end()
  7. #define SZ(x) ((int)(x).size())
  8. #define UM unordered_map
  9. #define PB push_back
  10. #define V vector
  11. #define EPS 1e-8
  12. typedef long long int LL;
  13. typedef string STR;
  14. /*FIN*/
  15.  
  16. int main()
  17. {
  18. FAST;
  19. int n;
  20. cin>>n;
  21. V<LL> a(n+1), s(n+1);
  22. for(auto &i:a) cin>>i;
  23. LL _t = 1LL;
  24. bool f=1;
  25. for(int i=0;i<=n;i++) {
  26. if(_t < 1e18) s[i] = _t - a[i], _t = 2LL*s[i] ;
  27. else s[i] = 1e18 ;
  28. if(s[i]<0) {
  29. f=0;
  30. break;
  31. }
  32. }
  33. if(a[n]==0) f=0;
  34. LL ans=0;
  35. if(f) {
  36. ans += a[n];
  37. for(int d = n-1; d>=0; d--) {
  38. if(a[d+1] > 2*s[d]) {
  39. f=0;
  40. break;
  41. }
  42. ans += min(a[d+1], s[d]) + a[d];
  43. a[d] += min(a[d+1],s[d]);
  44. }
  45. }
  46. if(f) cout<<ans<<endl;
  47. else cout<<"-1\n";
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 4296KB
stdin
Standard input is empty
stdout
-1