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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
// #include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i, x, n) for (int i = x; i < n; i++)
#define repd(i, x, n) for (int i = x; i <= n; i++)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define gbtb ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)                                                                 \\
  if (DEBUG_Switch)                                                            \\
  cout << "[" << #x << " " << (x) << "]" << endl
#define du3(a, b, c) scanf("%d %d %d", &(a), &(b), &(c))
#define du2(a, b) scanf("%d %d", &(a), &(b))
#define du1(a) scanf("%d", &(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll powmod(ll a, ll b, ll MOD) {
  if (a == 0ll) {
    return 0ll;
  }
  a %= MOD;
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
ll poww(ll a, ll b) {
  if (a == 0ll) {
    return 0ll;
  }
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
void Pv(const vector<int> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%d", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
void Pvl(const vector<ll> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%lld", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
inline long long readll() {
  long long tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
inline int readint() {
  int tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
void pvarr_int(int *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%d%c", arr[i], i == n ? '\\n' : ' '); }
}
void pvarr_LL(ll *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%lld%c", arr[i], i == n ? '\\n' : ' '); }
}
const int maxn = 100010;
const ll inf = 1e18;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
ll a[maxn];
ll b[maxn];
ll tree[maxn];
int lowbit(int x) { return (-x) & x; }
void add(int pos, ll val) {
  while (pos < maxn) {
    tree[pos] += val;
    pos += lowbit(pos);
  }
}
ll ask(int pos) {
  ll res = 0;
  while (pos > 0) {
    res += tree[pos];
    pos -= lowbit(pos);
  }
  return res;
}

int main() {
#if DEBUG_Switch
  freopen("C:\\\\code\\\\input.txt", "r", stdin);
#endif
  // freopen("C:\\\\code\\\\output.txt","r",stdin);
  n = readint();
  repd(i, 0, n) { a[i] = readint(); }
  if (n == 0) {
    if (a[0] == 1) {
      printf("1\\n");
    } else {
      printf("-1\\n");
    }
  } else {
    if (a[0] != 0) {
      printf("-1\\n");
    } else {
      b[0] = 1;
      int isok = 1;
      repd(i, 1, n) {
        ll x = b[i - 1] << 1;
        x = min(x, inf);
        if (a[i] > x) {
          isok = 0;
          break;
        } else {
          b[i] = x - a[i];
        }
      }
      if (isok) {
        // printf("Yes\\n");
        b[0]--;
        repd(i, 1, n) {
          b[i] += a[i];
          b[i]--;
          add(i, b[i] - b[i - 1]);
        }
        int id = 1;
        ll ans = n + 1;
        a[n]--;
        repd(i, 1, n) {
          while (a[i] > 0) {
            ll num = ask(id);
            while (num == 0 && id <= n) {
              id++;
              num = ask(id);
            }
            num = min(num, a[i]);
            ans += 1ll * (i - id + 1) * num;
            add(id, -num);
            add(i + 1, num);
            a[i] -= num;
          }
        }
        printf("%lld\\n", ans);
      } else {
        printf("-1\\n");
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <vector>
// #include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i, x, n) for (int i = x; i < n; i++)
#define repd(i, x, n) for (int i = x; i <= n; i++)
#define pii pair<int, int>
#define pll pair<long long, long long>
#define gbtb ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)                                                                 \\
  if (DEBUG_Switch)                                                            \\
  cout << "[" << #x << " " << (x) << "]" << endl
#define du3(a, b, c) scanf("%d %d %d", &(a), &(b), &(c))
#define du2(a, b) scanf("%d %d", &(a), &(b))
#define du1(a) scanf("%d", &(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll powmod(ll a, ll b, ll MOD) {
  if (a == 0ll) {
    return 0ll;
  }
  a %= MOD;
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a % MOD;
    }
    a = a * a % MOD;
    b >>= 1;
  }
  return ans;
}
ll poww(ll a, ll b) {
  if (a == 0ll) {
    return 0ll;
  }
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = ans * a;
    }
    a = a * a;
    b >>= 1;
  }
  return ans;
}
void Pv(const vector<int> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%d", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
void Pvl(const vector<ll> &V) {
  int Len = sz(V);
  for (int i = 0; i < Len; ++i) {
    printf("%lld", V[i]);
    if (i != Len - 1) {
      printf(" ");
    } else {
      printf("\\n");
    }
  }
}
inline long long readll() {
  long long tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
inline int readint() {
  int tmp = 0, fh = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fh = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    tmp = tmp * 10 + c - 48, c = getchar();
  return tmp * fh;
}
void pvarr_int(int *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%d%c", arr[i], i == n ? '\\n' : ' '); }
}
void pvarr_LL(ll *arr, int n, int strat = 1) {
  if (strat == 0) {
    n--;
  }
  repd(i, strat, n) { printf("%lld%c", arr[i], i == n ? '\\n' : ' '); }
}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
int a[maxn];
int b[maxn];
int tree[maxn];
int lowbit(int x) { return (-x) & x; }
void add(int pos, int val) {
  while (pos < maxn) {
    tree[pos] += val;
    pos += lowbit(pos);
  }
}
int ask(int pos) {
  int res = 0;
  while (pos > 0) {
    res += tree[pos];
    pos -= lowbit(pos);
  }
  return res;
}

int main() {
#if DEBUG_Switch
  freopen("C:\\\\code\\\\input.txt", "r", stdin);
#endif
  // freopen("C:\\\\code\\\\output.txt","r",stdin);
  n = readint();
  repd(i, 0, n) { a[i] = readint(); }
  if (n == 0) {
    if (a[0] == 1) {
      printf("1\\n");
    } else {
      printf("-1\\n");
    }
  } else {
    if (a[0] != 0) {
      printf("-1\\n");
    } else {
      b[0] = 1;
      int isok = 1;
      repd(i, 1, n) {
        int x = b[i - 1] << 1;
        x = min(x, inf);
        if (a[i] > x) {
          isok = 0;
          break;
        } else {
          b[i] = x - a[i];
        }
      }
      if (isok) {
        // printf("Yes\\n");
        b[0]--;
        repd(i, 1, n) {
          b[i] += a[i];
          b[i]--;
          add(i, b[i] - b[i - 1]);
          // printf("%d %d\\n", i, b[i] );
        }
        int id = 1;
        ll ans = n + 1;
        a[n]--;
        repd(i, 1, n) {
          while (a[i] > 0) {
            // chu(ask(id));
            while (ask(id) == 0 && id <= n) {
              id++;
            }
            ans += i - id + 1;
            add(id, -1);
            add(i + 1, 1);
            a[i]--;
          }
        }
        printf("%lld\\n", ans);
      } else {
        printf("-1\\n");
      }
    }
  }
  return 0;
}