Problem p02665 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define mem(x, y)                                                              \\
  memset(x, y,                                                                 \\
         sizeof(x)) // memset only use to initialize 0 or -1 no other number.
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e9
#define INFL 1e18;
#define mod 1000000007
// #define mod 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * x;
    }
    x = x * x;
    n = n / 2;
  }
  return res;
}
ll powm(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

// cout<< fixed << setprecision(10)

int main() {

  fast;
  int T;
  T = 1;
  // cin>>T;
  while (T--) {
    ll n, i;
    cin >> n;
    ll a[n + 1];
    for (i = 0; i < n + 1; i++) {
      cin >> a[i];
    }
    ll b[n + 1];
    b[n] = a[n];
    for (i = n - 1; i >= 0; i--) {
      b[i] = b[i + 1] + a[i];
    }
    ll c[n + 1];
    c[0] = 1;
    for (i = 1; i < n + 1; i++) {
      c[i] = min(b[i], 2 * (c[i - 1] - a[i - 1]));
    }
    int x = 1;
    ll ans = 0;
    for (i = 0; i < n + 1; i++) {
      if (c[i] < a[i]) {
        x = 0;
      }
      ans += c[i];
    }
    if (x == 0) {
      cout << -1 << "\\n";
    } else {
      cout << ans;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define mem(x, y)                                                              \\
  memset(x, y,                                                                 \\
         sizeof(x)) // memset only use to initialize 0 or -1 no other number.
#define pii pair<int, int>
#define pll pair<ll, ll>
#define INF 1e9
#define INFL 1e18;
#define mod 1000000007
// #define mod 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pii, null_type, less<pii>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;

ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = res * x;
    }
    x = x * x;
    n = n / 2;
  }
  return res;
}
ll powm(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}

// cout<< fixed << setprecision(10)

int main() {

  fast;
  int T;
  T = 1;
  // cin>>T;
  while (T--) {
    ll n, i;
    cin >> n;
    ll a[n + 1];
    for (i = 0; i < n + 1; i++) {
      cin >> a[i];
    }
    ll b[n + 1];
    b[n] = a[n];
    for (i = n - 1; i >= 0; i--) {
      b[i] = b[i + 1] + a[i];
    }
    ll c[n + 1];
    c[0] = 1;
    for (i = 1; i < n + 1; i--) {
      c[i] = min(b[i], 2 * (c[i - 1] - a[i]));
    }
    int x = 1;
    ll ans = 0;
    for (i = 0; i < n + 1; i++) {
      if (c[i] < a[i]) {
        x = 0;
      }
      ans += c[i];
    }
    if (x == 0) {
      cout << -1 << "\\n";
    } else {
      cout << ans;
    }
  }
}