# folia.cpp

Dec 9th, 2021
707
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <bits/stdc++.h>
2. using namespace std;
3.
4. #define int long long
5. #define vi vector<int>
6. #define ii pair<int,int>
7. #define fi first
8. #define sc second
9. #define stoi stoll
10. #define popcnt __builtin_popcount
11. #define getbit(x, k) ((x >> k) & 1)
12. #define all(x) (x).begin(),(x).end()
13. #define FOR(i,j,k) for(int i=j; i<k; i++)
14. #define look(a) cerr <<#a<<": "<<a<<endl;
15. #define look2(a,b) cerr <<#a<<": "<<a<<" | "<<#b<<": "<<b<< endl;
16.
17. void solve()
18. {
19.     int n;
20.     cin >> n;
21.     int a[n+1],b[n+1],c[n+1];
22.     FOR(i,0,n+1) cin >> a[i];
23.     if (a[0] > 1){
24.         cout << -1;
25.         return;
26.     }
27.     c[n] = a[n];
28.     for (int i = n-1; i >= 0; i--){
29.         c[i] = c[i+1] + a[i];
30.     }
31.     b[0] = 1 - a[0];
32.     FOR(i,1,n+1){
33.         if (a[i] > b[i-1]*2){
34.             cout << -1;
35.             return;
36.         }
37.         b[i] = min(c[i+1],b[i-1]*2 - a[i]);
38.         //look2(c[i+1],b[i-1]*2 - a[i])
39.         if (a[i] + b[i] < b[i-1]){
40.             cout << -1;
41.             return;
42.         }
43.     }
44.     //FOR(i,0,n+1) look2(a[i],b[i]);
45.     int s = a[n];
46.     FOR(i,0,n) s += a[i] + b[i];
47.     cout << s;
48. }
49.
50. signed main()
51. {
52.     cin.tie(0)->sync_with_stdio(0);
53.     freopen("in", "r", stdin);
54.     freopen("out", "w", stdout);
55.     int T = 1;
56.     // cin >> T;
57.     while (T--) {
58.         solve();
59.         cout << '\n';
60.     }
61.     cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
62. }
RAW Paste Data