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

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

// #include <boost/multiprecision/cpp_int.hpp>
// namespace mp = boost::multiprecision;
// // 任意長整数型
// using Bint = mp::cpp_int;

// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto &&i : n)

/* debug */
#define debug(x)                                                               \\
  cerr << x << " "                                                             \\
       << "(L:" << __LINE__ << ")" << '\\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
          dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {
  return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
  int x;
  cin >> x;
  return x;
}
inline ll IN() {
  ll x;
  cin >> x;
  return x;
}
inline vs split(const string &t, char c) {
  vs v;
  stringstream s(t);
  string b;
  while (getline(s, b, c))
    v.eb(b);
  return v;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b)
    a = b;
  return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b)
    a = b;
  return a < b;
}
template <typename T, typename S> inline void print(const pair<T, S> &p) {
  cout << p.first << " " << p.second << endl;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }
template <typename T, typename S>
inline void print(const vector<pair<T, S>> &v) {
  for (auto &&p : v)
    print(p);
}
template <typename T> inline void print(const vector<T> &v, string s = " ") {
  REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\\n");
}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {
  cout << (ok ? yes : no) << endl;
}

int a[100001];
ll space[100001];
ll used[100001];
// int lr[100001];
// int r[100001];

signed main() {
  // my-code
  int n;
  cin >> n;
  REP(i, n + 1) cin >> a[i];

  if (n == 0) {
    if (a[0] == 1) {
      print(1);
    } else {
      print(-1);
    }
    return 0;
  }

  ll ans = 0;
  space[0] = 1;
  used[0] = 0;

  REP(i, n + 1) {
    int leaf = a[i];
    if (i == 0) {
      if (leaf > 0) {
        print(-1);
        return 0;
      } else {
        continue;
      }
    }
    space[i] = space[i - 1] * 2;
    if (space[i] > 10000000000000001) {
      space[i] = 10000000000000001;
    }
    if (space[i] < leaf) {
      print(-1);
      return 0;
    } else {
      used[i] = leaf;
      space[i] -= leaf;
    }
  }

  // REP(i, n+1) {
  //     cout << used[i] << " " << space[i] << endl;
  // }

  ll acc = 0;
  DEP(i, n) {
    ans += min(space[i] + used[i], acc + used[i]);
    acc = min(space[i] + used[i], acc + used[i]);
    // print(acc);
  }

  print(ans);

  return 0;
}

// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々

Error: TLE

Fixed submission:

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

// #define int long long // intで書いたけど心配なときにlong longに変換する
struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;

/* short */
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) begin(v), end(v)
#define RALL(v) rbegin(v), rend(v)
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto &&i : n)

/* debug */
#define debug(x)                                                               \\
  cerr << x << " "                                                             \\
       << "(L:" << __LINE__ << ")" << '\\n';

/* alias */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
using vs = vector<string>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;

/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
          dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {
  return y >= 0 && x >= 0 && y < H && x < W;
}
inline int in() {
  int x;
  cin >> x;
  return x;
}
inline ll IN() {
  ll x;
  cin >> x;
  return x;
}
inline vs split(const string &t, char c) {
  vs v;
  stringstream s(t);
  string b;
  while (getline(s, b, c))
    v.eb(b);
  return v;
}
template <typename T> inline bool chmin(T &a, const T &b) {
  if (a > b)
    a = b;
  return a > b;
}
template <typename T> inline bool chmax(T &a, const T &b) {
  if (a < b)
    a = b;
  return a < b;
}
template <typename T, typename S> inline void print(const pair<T, S> &p) {
  cout << p.first << " " << p.second << endl;
}
template <typename T> inline void print(const T &x) { cout << x << endl; }
template <typename T, typename S>
inline void print(const vector<pair<T, S>> &v) {
  for (auto &&p : v)
    print(p);
}
template <typename T> inline void print(const vector<T> &v, string s = " ") {
  REP(i, v.size()) cout << v[i] << (i != (ll)v.size() - 1 ? s : "\\n");
}
inline void yesno(bool ok, string yes = "Yes", string no = "No") {
  cout << (ok ? yes : no) << endl;
}

int a[100001];

signed main() {
  // my-code
  int n;
  cin >> n;
  REP(i, n + 1) cin >> a[i];

  if (n == 0) {
    if (a[0] == 1) {
      print(1);
    } else {
      print(-1);
    }
    return 0;
  }

  minPQ<tuple<int, bool, bool>> pq; // depth, c1, c2
  ll ans = 0;
  pq.push(make_tuple(0, false, false));
  tuple<int, bool, bool> v;
  int depth;
  bool left, right;
  REP(i, n + 1) {
    int leaf = a[i];
    if (i == 0) {
      if (leaf > 0) {
        print(-1);
        return 0;
      } else {
        continue;
      }
    }
    REP(j, leaf) {
      if (pq.empty()) {
        print(-1);
        return 0;
      }
      v = pq.top();
      pq.pop();
      tie(depth, left, right) = v;
      // cout << "pop" << depth << " " << left << " " << right << endl;  //
      // DEBUG
      if (!left) {
        pq.push(make_tuple(depth, true, false));
        // cout << "push" << depth << " " << true << " " << false << endl;  //
        // DEBUG
      } else {
        // pq.push(make_tuple(depth, true, true));
        ans++;
        // cout << "push(del)" << depth << " " << false << " " << false << endl;
        // // DEBUG
      }
      REPS(k, depth + 1, i) {
        pq.push(make_tuple(k, true, false));
        // cout << "push" << k << " " << true << " " << false << endl;  // DEBUG
      }
      ans++;
    }
  }
  while (!pq.empty()) {
    // v = pq.top();
    pq.pop();
    // tie(depth, left, right) = v;
    // cout << "pop" << depth << " " << left << " " << right << endl;  // DEBUG
    ans++;
  }

  print(ans);

  return 0;
}

// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々