alexjercan/bugnet huggingface dataset:
Problem p02664 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
#define int long long
#define pb(x) push_back(x)
#define gcd(a, b) __gcd(a, b)
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b) / gcd(a, b)
#define bits(x) __builtin_popcountll(x)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int32_t main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("goodluckin.txt", "r", stdin);
  freopen("goodluckout.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '?') {
      s[i] = 'D';
    }
  }
  cout << s;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#define mod 1000000007
#define int long long
#define pb(x) push_back(x)
#define gcd(a, b) __gcd(a, b)
#define all(v) v.begin(), v.end()
#define lcm(a, b) (a * b) / gcd(a, b)
#define bits(x) __builtin_popcountll(x)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int32_t main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("goodluckin.txt", "r", stdin);
  freopen("goodluckout.txt", "w", stdout);
#endif
  string s;
  cin >> s;
  int n = s.size();
  for (int i = 0; i < s.size();) {
    if (s[i] == '?') {
      s[i] = 'D';
    }
  }
  cout << s;
}
Problem p02664 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  string s, t;
  cin >> s;
  t = s;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '?')
      t[i] = 'D';

  cout << t << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  t = "";
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] != '?')
      t = t + s[i];
    else
      t = t + 'D';
  }
  cout << t << endl;
}
Problem p02664 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define print(n) cout << n << endl
#define in(v)                                                                  \\
  int v;                                                                       \\
  cin >> v
#define vint vector<int>
#define pb push_back
typedef long long int ll;

int main() {
  string t;
  cin >> t;
  if (t.size() == 1) {
    if (t[0] == '?') {
      t[0] = 'D';
    }
    print(t);
    return 0;
  }
  for (int i = 0; i < t.size() - 2; i++) {
    if (t[i + 1] == '?') {
      if (t[i] == 'P') {
        t[i + 1] = 'D';
      } else if (t[i] == 'D') {
        if (t[i + 2] == '?') {
          t[i + 2] = 'D';
          t[i + 1] = 'P';
        } else if (t[i + 2] == 'D') {
          t[i + 1] = 'P';
        } else {
          t[i + 1] = 'D';
        }
      } else {
        t[i + 1] = 'D';
      }
    }
  }
  if (t[0] == '?')
    t[0] = 'D';
  if (t[t.size() - 1] == '?') {
    t[t.size() - 1] = 'D';
  }
  print(t);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define print(n) cout << n << endl
#define in(v)                                                                  \\
  int v;                                                                       \\
  cin >> v
#define vint vector<int>
#define pb push_back
typedef long long int ll;
typedef unsigned long long int ull;
#define swap(a, b)                                                             \\
  do {                                                                         \\
    a = a ^ b;                                                                 \\
    b = a ^ b;                                                                 \\
    a = a ^ b;                                                                 \\
  } while (0)

int main() {
  string t;
  cin >> t;
  rep(i, t.size() - 2) {
    if (t[i + 1] == '?') {
      if (t[i] == 'P') {
        t[i + 1] = 'D';
      } else if (t[i] == 'D') {
        if (t[i + 2] == '?') {
          t[i + 2] = 'D';
          t[i + 1] = 'P';
        } else if (t[i + 2] == 'D') {
          t[i + 1] = 'P';
        } else {
          t[i + 1] = 'D';
        }
      } else if (t[i] == 'P') {
      }
    }
  }
  if (t[0] == '?')
    t[0] = 'D';
  if (t[t.size() - 1] == '?') {
    t[t.size() - 1] = 'D';
  }
  print(t);
  return 0;
}
Problem p02664 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string t;
  cin >> t;
  int i;
  for (i = 0; i < (int)t.size(); i++) {
    if (t.at(i) == '?') {
      t.at(i) = 'D';
    }
  }
  cout << t << endl;
}

Error: 0

Fixed submission:

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

int main() {
  string t;
  cin >> t;
  int i;
  for (i = 0; i < (int)t.size(); i++) {
    if (t.at(i) == '?') {
      if (t.at(i - 1) == 'P') {
        t.at(i) = 'D';
      } else if (t.at(i - 1) == 'D') {
        t.at(i) = 'P';
      }
    }
  }
  cout << t << endl;
}
Problem p02664 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  string t;
  cin >> t;

  for (int i = 0; i < t.size(); ++i) {
    if (t[i] == '?') {
      t[i] = 'D';
    }
  }

  cout << t << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int str_point(string s) {
  int cnt = 0;

  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == 'P' and s[i + 1] == 'D') {
      cnt++;
    }

    if (s[i] == 'D') {
      cnt++;
    }
  }

  return cnt;
}

int main() {
  string t;
  cin >> t;

  int p = 0;
  for (int i = 0; i < t.size(); ++i) {
    p += str_point(t);

    if (t[i] == '?') {
      t[i] = 'P';
      int x = str_point(t);

      t[i] = 'D';
      int y = str_point(t);

      if (x < y) {
        t[i] = 'D';
      } else {
        t[i] = 'P';
      }
    }
  }
  cout << t << endl;

  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define REP(i, init, n) for (int i = (int)(init); i < (int)(n); i++)

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>
long INF = (1LL << 60);

int main() {
  int N;
  cin >> N;
  vi A(N + 1);
  REP(i, 0, N + 1) { cin >> A[i]; }

  if (N == 0) {
    if (A[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  vl maxN(N + 1, 0);
  vl node(N + 1, 0);
  maxN[0] = 1;

  REP(i, 1, N + 1) { maxN[i] = (long)min((maxN[i - 1] - A[i - 1]) * 2, INF); }
  /*
  REP(i, 0, N+1) cout << maxN[i] << " ";
  cout << endl;
  */
  node[N] = A[N];
  for (int i = N - 1; i >= 0; i--) {
    if (maxN[i] >= node[i + 1] + A[i]) {
      node[i] = node[i + 1] + A[i];
    } else if (maxN[i] >= (long)(node[i + 1] + 1) / 2 + A[i]) {
      node[i] = maxN[i];
    } else {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << accumulate(node.begin(), node.end(), 0LL) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, init, n) for (int i = (int)(init); i < (int)(n); i++)

#define vi vector<int>
#define vl vector<long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long>>
#define pint pair<int, int>
#define plong pair<long, long>
long INF = (1LL << 60);

int main() {
  int N;
  cin >> N;
  vi A(N + 5);
  REP(i, 0, N + 1) { cin >> A[i]; }

  if (N == 0) {
    if (A[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  vl maxN(N + 5, 0);
  vl node(N + 5, 0);
  maxN[0] = 1;

  REP(i, 1, N + 1) { maxN[i] = (long)min((maxN[i - 1] - A[i - 1]) * 2, INF); }
  /*
  REP(i, 0, N+1) cout << maxN[i] << " ";
  cout << endl;
  */
  node[N] = A[N];
  for (int i = N - 1; i >= 0; i--) {
    if (maxN[i] >= node[i + 1] + A[i]) {
      node[i] = node[i + 1] + A[i];
    } else if (maxN[i] >= (long)(node[i + 1] + 1) / 2 + A[i]) {
      node[i] = maxN[i];
    } else {
      cout << -1 << endl;
      return 1;
    }
  }

  cout << accumulate(node.begin(), node.end(), 0LL) << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n = 0;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 0; i < n + 1; i++)
    cin >> a[i];
  if (n == 0) {
    if (a[0] == 1)
      cout << 1 << endl;
    else
      cout << -1 << endl;
    return 0;
  }
  ll res = 0;
  // vector<ll> x(n + 1);
  vector<ll> y(n + 1);
  y[0] = 1;
  vector<ll> wa(n + 1);
  // x[0] = 0,y[0] = 0;
  if (a[0] != 0) {
    cout << -1 << endl;
    return 0;
  }
  wa[n] = a[n];
  for (int i = n - 1; i >= 0; i--)
    wa[i] = wa[i + 1] + a[i] /*,cout << wa[i] << " "*/;
  // cout << endl;
  for (int i = 1; i < n + 1; i++) {
    ll now = 0;
    now = y[i - 1] * 2 - a[i];
    // cout << y[i - 1] * 2 << " " << a[i] << endl;
    if (wa[i] - a[i] < 0 || now < 0) {
      cout << -1 << endl;
      return 0;
    }
    now = min(now, wa[i] - a[i]);
    y[i] = now;
  }
  for (int i = 0; i < n + 1; i++)
    res += a[i] + y[i];
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
  ll n = 0;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 0; i < n + 1; i++)
    cin >> a[i];
  ll res = 0;
  ll now = 0;
  now = 1;
  bool can = true;
  vector<ll> leaf;
  for (int i = 0; i < n + 1; i++) {
    if ((i == 0 && a[i] > 1) || a[i] > now * 2) {
      can = false;
      break;
    }
    if (i != 0)
      now *= 2;
    res += now;
    now -= a[i];
    leaf.push_back(now);
    // cout << now << " " << res << endl;
  }
  // cout << res << endl;
  if (leaf[leaf.size() - 1] > a[n]) {
    ll memo = 0;
    // cout << memo << " " << leaf[leaf.size() - 1] << " " << a[n] << endl;
    ll size = 0;
    size = leaf.size();
    for (int i = size - 1; i >= 0; i--) {
      // cout << leaf[i] << " " << max((ll)0,leaf[i] - memo) << endl;
      // res -= max((ll)0,leaf[i] - memo);
      /*if(leaf[i] > memo * 2)*/ res -= max((ll)0, leaf[i] - memo);
      // cout << res << endl;
      memo += a[i];
    }
    // res -= a[n] * 2;
    // cout << a[n] << endl;
  }
  if (!can)
    cout << -1 << endl;
  else
    cout << res << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define DB cerr << "D" << endl
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; }
using P = pair<double, int>;
const ll M = 1e9 + 7;

ll pou(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1)
      an = an * x;
    x = x * x;
    a >>= 1;
  }
  return an;
}

int main() {
  int n;
  cin >> n;
  ll a[n + 1];
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  ll sm = 0;
  for (int i = 0; i <= n; i++)
    sm += a[i];

  ll nw = 1, an = 0;
  for (ll i = 0; i <= n; i++) {
    an += nw;
    nw -= a[i];
    sm -= a[i];
    // cout<<i<<" "<<an<<" "<<nw<<" "<<sm<<endl;
    if (nw < 0) {
      cout << -1 << endl;
      return 0;
    }
    nw = min(2 * nw, sm);
  }
  cout << an << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define DB cerr << "D" << endl
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; }
using P = pair<double, int>;
const ll M = 1e9 + 7;

ll pou(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1)
      an = an * x;
    x = x * x;
    a >>= 1;
  }
  return an;
}

int main() {
  int n;
  cin >> n;
  ll a[n];
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  ll sm = 0;
  for (int i = 0; i <= n; i++)
    sm += a[i];

  ll nw = 1, an = 0;
  for (ll i = 0; i <= n; i++) {
    an += nw;
    nw -= a[i];
    sm -= a[i];
    // cout<<i<<" "<<an<<" "<<nw<<" "<<sm<<endl;
    if (nw < 0) {
      cout << -1 << endl;
      return 0;
    }
    nw = min(2 * nw, sm);
  }
  cout << an << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\\n";
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

#line 2 "nomura2020_c.cpp"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vl a(n + 1);
  REP(i, n + 1) { cin >> a[i]; }

  if (n == 0) {
    cout << (a[0] == 1 ? 1 : -1) << endl;
    return 0;
  }

  vl par(n, 0), node(n + 1, 0);
  par[0] = 1 - a[0];
  FOR(i, 1, n) {
    if (par[i - 1] > 1e15) {
      par[i] = 1e18;
    } else {
      par[i] = 2LL * par[i - 1] - a[i];
      if (par[i] < 0) {
        cout << -1 << endl;
        return 0;
      }
    }
  }

  node[n] = a[n];
  for (int i = n - 1; i >= 0; i--) {
    node[i] = a[i];
    if (par[i] > node[i + 1]) {
      node[i] += node[i + 1];
    } else {
      if (2 * par[i] < node[i + 1]) {
        cout << -1 << endl;
        return 0;
      }
      node[i] += par[i];
    }
  }

  ll ans = 0;
  REP(i, n + 1) ans += node[i];
  cout << ans << endl;
}


Error: 0

Fixed submission:

#line 1 "/mnt/c/Users/leafc/dev/compro/lib/template.hpp"

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes = "Yes", T no = "No") {
  cout << (val ? yes : no) << "\\n";
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

#line 2 "nomura2020_c.cpp"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vl a(n + 1);
  REP(i, n + 1) { cin >> a[i]; }

  vl par(n, 0), node(n + 1, 0);
  par[0] = 1 - a[0];
  FOR(i, 1, n) {
    par[i] = 2 * par[i - 1] - a[i];
    if (par[i] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  node[n] = a[n];
  for (int i = n - 1; i >= 0; i--) {
    node[i] = a[i];
    if (par[i] > node[i + 1])
      node[i] += node[i + 1];
    else {
      if (2 * par[i] < node[i + 1]) {
        cout << -1 << endl;
        return 0;
      }
      node[i] += par[i];
    }
  }

  ll ans = 0;
  REP(i, n + 1) ans += node[i];
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {
#ifdef LOCAL_EXEC
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif // LOCAL_EXEC
  vector<long long int> vt;
  long long int i, n, j;
  cin >> n;
  long long int A[n + 1];
  bool ok = true;
  vt.push_back(1);
  for (i = 0; i <= n; i++) {
    cin >> A[i];
    if (A[i] > vt[vt.size() - 1])
      ok = false;
    if ((vt[i] - A[i]) * 2 < 1e18)
      vt.push_back(max(0ll, (vt[i] - A[i]) * 2));
    else
      vt.push_back(1e18);
  }
  // for(i=0;i<vt.size();i++){
  //   cout<<vt[i]<<endl;
  //}
  // cout<<vt.size()<<endl;
  if (n == 0 && A[0] != 1)
    ok = false;
  if (ok == false)
    cout << -1;
  else {
    long long int sum = 0, dp[n + 1];
    // cout<<ok<<endl;
    dp[n] = A[n];
    for (i = n - 1; i >= 0; i--) {
      dp[i] = A[i] + dp[i + 1];
      // cout<<vt[i]<<" ";
      dp[i] = min(vt[i], dp[i]);
    }
    for (i = 0; i <= n; i++) {
      sum += dp[i];
    }
    cout << sum;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

int main() {
#ifdef LOCAL_EXEC
#else
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#endif // LOCAL_EXEC
  vector<long long int> vt;
  long long int i, n, j;
  vt.push_back(1);
  for (i = 1; i <= 62; i++) {
    vt.push_back(vt[vt.size() - 1] * 2);
  }
  cin >> n;
  long long int A[n + 1];
  bool ok = true;
  for (i = 0; i <= n; i++) {
    cin >> A[i];
    if (A[i] > vt[i])
      ok = false;
    else if (A[i] == vt[i] && i < n)
      ok = false;
  }
  if (n == 0 && A[0] != 1)
    ok = false;
  if (ok == false)
    cout << -1;
  else {
    long long int sum = 0, dp[n + 1];
    // cout<<ok<<endl;
    for (i = 1; i < vt.size(); i++) {
      if (i > n)
        break;
      vt[i] = (vt[i - 1] - A[i - 1]) * 2;
      if (i <= n && A[i] > vt[i])
        ok = false;
      // cout<<A[i]<<" "<<vt[i]<<endl;
    }
    if (ok == false)
      cout << -1;
    else {
      for (i = n; i >= 0; i--) {
        if (i > 62) {
          if (i < n)
            A[i] += A[i + 1];
          dp[i] = A[i];
        } else {
          if (i == n)
            dp[i] = min(A[i], vt[i]);
          else {
            A[i] += A[i + 1];
            dp[i] = min(vt[i], A[i]);
          }
        }
      }
      for (i = 0; i <= n; i++) {
        sum += dp[i];
      }
      cout << sum;
    }
  }
  return 0;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  size_t N;
  cin >> N;

  vector<long long> A(N + 1);
  for (long long &a : A) {
    cin >> a;
  }

  vector<long long> dp(N + 1, 0);
  dp[N] = A[N];
  vector<long long> limit(N + 1, 0);
  limit[0] = 1 - A[0];
  if (limit[0] < 0) {
    cout << -1 << endl;
    return 0;
  }
  for (size_t i = 1; i <= N; i++) {
    limit[i] = min(2 * limit[i - 1] - A[i], (long long)1e17);
    if (limit[i] < 0) {
      cout << -1 << endl;
      return 0;
    }
  }

  long long ans = dp[N];
  for (size_t i = N - 1; i < N; i--) {
    long long target = dp[i + 1];
    if (2 * limit[i] < target) {
      cout << -1 << endl;
      return 0;
    }
    dp[i] = min(target, limit[i]) + A[i];
    ans += dp[i];
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  long long N;
  cin >> N;

  vector<long long> A(N + 1);
  for (long long &a : A) {
    cin >> a;
  }

  vector<vector<long long>> dp(N + 1, vector<long long>(3, 0));
  // dp[i][0] := A[i] == # of nodes which has no children in depth i
  // dp[i][1] := # of nodes which has one child in depth i
  // dp[i][2] := # of nodes which has two children in depth i
  // sum_{j=0}^{2} dp[i][j] = dp[i - 1][1] + 2 x dp[i - 1][2] <= 2^{i}
  for (long long i = 0; i <= N; i++) {
    dp[i][0] = A[i];
  }

  vector<long long> limit(N + 1, 0);
  limit[0] = 1 - A[0];
  for (long long i = 1; i <= N; i++) {
    limit[i] = 2 * limit[i - 1] - A[i];
    if (limit[i] < 0) {
      cout << -1 << endl;
    }
  }

  long long ans = dp[N][0];
  for (long long i = N - 1; i >= 0; i--) {
    long long target = dp[i + 1][0] + dp[i + 1][1] + dp[i + 1][2];
    if (2 * limit[i] < target) {
      cout << -1 << endl;
      return 0;
    }
    dp[i][2] = max(0ll, target - limit[i]);
    dp[i][1] = target - 2 * dp[i][2];

    assert(dp[i][1] + dp[i][2] <= limit[i]);
    if (limit[i] - dp[i][1] - dp[i][2] > 0) {
      assert(dp[i][2] == 0);
    }

    ans += dp[i][0] + dp[i][1] + dp[i][2];
  }
  cout << ans << endl;
}
Problem p02665 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
constexpr long long int mod = 1000000007, MOD = 998244353;
constexpr double PI = 3.14159265358979323846;

int main() {
  long long N;
  cin >> N;
  vector<long long> U(N + 1), Usum(N + 2);
  for (long long i = 0; i <= N; i++) {
    cin >> U.at(i);
  }
  for (long long i = 1; i <= N + 1; i++) {
    Usum.at(i) = Usum.at(i - 1) + U.at(i - 1);
  }

  vector<long long> maxe(N + 1); // たれてるふんどし
  maxe.at(0) = 1;

  for (long long i = 0; i < N; i++) {
    if (maxe.at(i) < U.at(i)) {
      cout << -1;
      return 0;
    } else {
      // cout << Usum.at(N)-Usum.at(i) << endl;
      maxe.at(i + 1) = min(
          (maxe.at(i) - U.at(i)) * 2,
          Usum.at(N + 1) -
              Usum.at(
                  i +
                  1)); // 早いうちに広げる，でも広げすぎると片付けられなくなる
      if (maxe.at(i + 1) == 0) {
        cout << -1;
        return 0;
      }
    }
    maxe.at(N) = 0;
  }
  long long ans = 0;
  for (long long i = 0; i <= N - 1; i++) {
    // cout << maxe.at(i) << endl;
    ans += maxe.at(i);
  }

  ans += U.at(N);
  if (N == 0 && U.at(0) != 1)
    ans = -1;
  if (N != 0 && ((maxe.at(N - 1) - U.at(N - 1)) * 2 < U.at(N) || U.at(N) <= 0))
    ans = -1;
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
constexpr long long int mod = 1000000007, MOD = 998244353;
constexpr double PI = 3.14159265358979323846;

int main() {
  long long N;
  cin >> N;
  vector<long long> U(N + 1), Usum(N + 2);
  for (long long i = 0; i <= N; i++) {
    cin >> U.at(i);
  }
  for (long long i = 1; i <= N + 1; i++) {
    Usum.at(i) = Usum.at(i - 1) + U.at(i - 1);
  }

  vector<long long> maxe(N + 1); // たれてるふんどし
  maxe.at(0) = 1;

  for (long long i = 0; i < N; i++) {
    if (maxe.at(i) < U.at(i)) {
      cout << -1;
      return 0;
    } else {
      // cout << Usum.at(N)-Usum.at(i) << endl;
      maxe.at(i + 1) = min(
          (maxe.at(i) - U.at(i)) * 2,
          Usum.at(N + 1) -
              Usum.at(
                  i +
                  1)); // 早いうちに広げる，でも広げすぎると片付けられなくなる
      if (maxe.at(i + 1) == 0) {
        cout << -1;
        return 0;
      }
    }
    maxe.at(N) = 0;
  }
  long long ans = 0;
  for (long long i = 0; i <= N - 1; i++) {
    // cout << maxe.at(i) << endl;
    ans += maxe.at(i);
  }

  ans += U.at(N);
  if (N == 0 && U.at(0) != 1)
    ans = -1;
  if ((maxe.at(N - 1) - U.at(N - 1)) * 2 < U.at(N) || U.at(N) <= 0)
    ans = -1;
  cout << ans;
}