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

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

#define endl '\\n'
#define ff first
#define ss second
#define sz(v) (int)v.size()
#define int long long
#define pb push_back
#define f(i, l, r) for (ll i = l; i <= r; i++)
#define rf(i, r, l) for (ll i = r; i >= l; i--)
#define Max 5000001
#define inf 2147483647
#define llinf 4223372036854775807
#define all(v) v.begin(), v.end()
#define bp(n) __builtin_popcountll(n)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define d0(a) cout << a << ' ';
#define d1(a) cout << a << endl;
#define d2(a, b) cout << a << ' ' << b << endl;
#define d3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define d4(a, b, c, d) cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
#define d5(a, b, c, d, e)                                                      \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
#define d6(a, b, c, d, e, f)                                                   \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << endl;

const ll N = 1e5 + 5, mod = 1e9 + 7, bit = 61;

ll a[N], n, on[N];

signed main() {
  fast;

  ll t = 1;
  // cin>>t;
  while (t--) {
    cin >> n;
    f(i, 0, n) { cin >> a[i]; }
    if (n > 0 and a[0] != 0) {
      cout << -1 << endl;
      continue;
    }
    on[0] = 1;
    f(i, 1, n) { on[i] = min((ll)llinf, 2 * (on[i - 1] - a[i - 1])); }
    ll bl = 0, ans = 0;
    rf(i, n, 0) {
      ll l = (bl + 1) / 2;
      ll r = bl;
      l += a[i];
      r += a[i];
      ll my = min(on[i], r);
      if (my < l) {
        ans = -1;
        break;
      }
      bl = my;
      ans += my;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;

#define endl '\\n'
#define ff first
#define ss second
#define sz(v) (int)v.size()
#define int long long
#define pb push_back
#define f(i, l, r) for (ll i = l; i <= r; i++)
#define rf(i, r, l) for (ll i = r; i >= l; i--)
#define Max 5000001
#define inf 2147483647
#define llinf 9223372036854775807
#define all(v) v.begin(), v.end()
#define bp(n) __builtin_popcountll(n)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define d0(a) cout << a << ' ';
#define d1(a) cout << a << endl;
#define d2(a, b) cout << a << ' ' << b << endl;
#define d3(a, b, c) cout << a << ' ' << b << ' ' << c << endl;
#define d4(a, b, c, d) cout << a << ' ' << b << ' ' << c << ' ' << d << endl;
#define d5(a, b, c, d, e)                                                      \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << endl;
#define d6(a, b, c, d, e, f)                                                   \\
  cout << a << ' ' << b << ' ' << c << ' ' << d << ' ' << e << ' ' << f << endl;

const ll N = 1e5 + 5, mod = 1e9 + 7, bit = 61;

ll a[N], n;

bool check(ll nw, ll d) {
  if (nw <= 1) {
    return 1;
  }
  ll p = nw;
  for (int i = d - 1; i >= 0; i--) {
    nw = (p + 1) / 2 + a[i];
    if (nw <= 1) {
      return 1;
    }
    p = nw;
  }
  return 0;
}

signed main() {
  fast;

  ll t = 1;
  // cin>>t;
  while (t--) {
    cin >> n;
    f(i, 0, n) { cin >> a[i]; }
    ll bl = 0, ans = 0;
    rf(i, n, 0) {
      ll my = -1;
      ll l = (bl + 1) / 2;
      ll r = bl;
      l += a[i];
      r += a[i];
      while (l <= r) {
        ll mid = (l + r) >> 1;
        if (check(mid, i)) {
          my = mid;
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
      if (my == -1) {
        ans = -1;
        break;
      }
      bl = my;
      ans += my;
    }
    cout << ans << endl;
  }
  return 0;
}