prithivMLmods/Coder-Stat huggingface dataset:
Problem p02665 (C++)

Buggy submission (status: 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];
  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;
}

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 + 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;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  unsigned int n;
  unsigned long ans = 0;
  unsigned long now;
  double check;
  bool solved = true;
  int i, j, k;

  cin >> n;
  unsigned int *a = new unsigned int[n + 1];
  unsigned long *top = new unsigned long[n + 1];

  cin >> a[0];
  if (a[0] > 1) {
    top[0] = 0;
    solved = false;
  } else {
    top[0] = 1 - a[0];
  }
  for (i = 1; i < n; i++) {
    cin >> a[i];
    if (top[i - 1] < a[i - 1]) {
      solved = false;
      top[i] = 0;
    } else {
      top[i] = (top[i - 1] - a[i - 1]) * 2;
    }
    if (top[i] < a[i]) {
      solved = false;
    }
    //		top[i] = top[i] - a[i-1];
    //		cout << i << " -> " << top[i] << endl;
  }

  if (n > 0) {
    cin >> a[n];
  }
  if (a[n] > top[n - 1] * 2) {
    solved = false;
  } else {
    top[n] = a[n];
  }

  if (solved == false) {
    cout << -1 << endl;
    return 0;
  }

  ans = now = a[n];
  for (i = n - 1; i >= 0; i--) {
    now += a[i];
    //		cout << i << ": " << now << " / " << top[i] << endl;
    now = min(now, top[i]);
    ans += now;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  unsigned int n;
  unsigned long ans = 0;
  unsigned long now;
  double check;
  bool solved = true;
  int i, j, k;

  cin >> n;
  unsigned int *a = new unsigned int[n + 1];
  unsigned long *top = new unsigned long[n + 1];

  cin >> a[0];
  if (a[0] > 1) {
    top[0] = 0;
    solved = false;
  } else {
    top[0] = 1 - a[0];
  }
  for (i = 1; i < n; i++) {
    cin >> a[i];
    if (top[i - 1] < a[i - 1]) {
      solved = false;
      top[i] = 0;
    } else {
      top[i] = (top[i - 1] - a[i - 1]) * 2;
    }
    if (top[i] < a[i]) {
      solved = false;
    }
    //		top[i] = top[i] - a[i-1];
    //		cout << i << " -> " << top[i] << endl;
  }

  if (n > 0) {
    cin >> a[n];
    if (a[n] > top[n - 1] * 2) {
      solved = false;
    } else {
      top[n] = a[n];
    }
  }

  if (solved == false) {
    cout << -1 << endl;
    return 0;
  }

  ans = now = a[n];
  for (i = n - 1; i >= 0; i--) {
    now += a[i];
    //		cout << i << ": " << now << " / " << top[i] << endl;
    now = min(now, top[i]);
    ans += now;
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100000];
long long ruia[100001];
long long ans = 0;
long long nowleef = 1;
int main() {
  cin >> n;
  for (long long i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n + 1; i++) {
    ruia[n - i] = ruia[n - i + 1] + a[n - i];
  }
  for (long long i = 0; i < n + 1; i++) {
    if (nowleef > ruia[i]) {
      ans = -1;
      break;
    }
    ans += nowleef;
    if (nowleef < a[i]) {
      ans = -1;
      break;
    }
    nowleef -= a[i];
    nowleef = min(nowleef * 2LL, ruia[i + 1]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n;
long long a[100005];
long long ruia[100006];
long long ans = 0;
long long nowleef = 1;
int main() {
  cin >> n;
  for (long long i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (long long i = 0; i < n + 1; i++) {
    ruia[n - i] = ruia[n - i + 1] + a[n - i];
  }
  for (long long i = 0; i < n + 1; i++) {
    if (nowleef > ruia[i]) {
      ans = -1;
      break;
    }
    ans += nowleef;
    if (nowleef < a[i]) {
      ans = -1;
      break;
    }
    nowleef -= a[i];
    nowleef = min(nowleef * 2LL, ruia[i + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  if (a[0] != 0) {
    cout << "-1" << endl;
    return -1;
  }
  ll ans = 0;
  vector<ll> num(n + 1);
  for (ll i = n; i >= 0; i--) {
    // ans += a[i] * (i+1);
    if (i == n)
      num[i] = a[i];
    else
      num[i] = num[i + 1] + a[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      // ans -= (num[i]-1);
      num[i] = 1;
      continue;
    }
    ll limit = (num[i - 1] - a[i - 1]) * 2LL;
    ll prev = num[i];
    num[i] = min(num[i], limit);
    if (num[i] <= a[i]) {
      cout << "-1" << endl;
      return 0;
    }
    if (i == n - 1) {
      if (num[i] - a[i] > a[n])
        num[i] = a[i] + a[n];

      if ((num[i] - a[i]) * 2 < a[n]) {
        cout << "-1" << endl;
        return 0;
      }
    }
    // ans -= (prev-num[i]);
  }
  rep(i, n + 1) { ans += num[i]; }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<ll, ll>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const ll mod = 1000000007;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  if (a[0] != 0) {
    if (a[0] == 1 && n == 0) {
      cout << 1 << endl;
    }

    else {
      cout << -1 << endl;
    }
    return 0;
  }
  ll ans = 0;
  vector<ll> num(n + 1);
  for (ll i = n; i >= 0; i--) {
    // ans += a[i] * (i+1);
    if (i == n)
      num[i] = a[i];
    else
      num[i] = num[i + 1] + a[i];
  }
  for (ll i = 0; i < n; i++) {
    if (i == 0) {
      // ans -= (num[i]-1);
      num[i] = 1;
      continue;
    }
    ll limit = (num[i - 1] - a[i - 1]) * 2LL;
    ll prev = num[i];
    num[i] = min(num[i], limit);
    if (num[i] <= a[i]) {
      cout << "-1" << endl;
      return 0;
    }
    if (i == n - 1) {
      if (num[i] - a[i] > a[n])
        num[i] = a[i] + a[n];

      if ((num[i] - a[i]) * 2 < a[n]) {
        cout << "-1" << endl;
        return 0;
      }
    }
    // ans -= (prev-num[i]);
  }
  rep(i, n + 1) { ans += num[i]; }

  cout << ans << endl;
  return 0;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
// __uint128_t

int main() {
  ll N;
  cin >> N;
  ll SUM = 0;
  vector<ll> C(N + 1);
  for (int i = 0; i < N + 1; i++)
    cin >> C[i];
  if (C[N] == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> P(N + 1);
  P[N] = 0, P[N - 1] = C[N];

  for (int i = N - 1; i >= 0; i--) {
    P[i] = C[i + 1] + P[i + 1];
  }

  ll temp = 1;
  for (int i = 0; i <= N; i++) {
    P[i] = min(temp - C[i], P[i]);
    temp = 2 * P[i];
    if (temp <= -1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < N + 1; i++)
    SUM += C[i] + P[i];
  cout << SUM << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
// __uint128_t

int main() {
  ll N;
  cin >> N;
  ll SUM = 0;
  vector<ll> C(N + 1);
  for (int i = 0; i < N + 1; i++)
    cin >> C[i];
  if (C[N] == 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<ll> P(N + 1);
  P[N] = 0;

  for (int i = N - 1; i >= 0; i--) {
    P[i] = C[i + 1] + P[i + 1];
  }

  ll temp = 1;
  for (int i = 0; i <= N; i++) {
    P[i] = min(temp - C[i], P[i]);
    temp = 2 * P[i];
    if (temp <= -1) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = 0; i < N + 1; i++)
    SUM += C[i] + P[i];
  cout << SUM << endl;
}
Problem p02665 (C++)

Buggy submission (status: 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 ((maxe.at(N - 1) - U.at(N - 1)) * 2 < U.at(N) || U.at(N) <= 0)
    ans = -1;
  cout << ans;
}

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 (N != 0 && ((maxe.at(N - 1) - U.at(N - 1)) * 2 < U.at(N) || U.at(N) <= 0))
    ans = -1;
  cout << ans;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const ll INF = 1e18;
vector<ll> two;

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

  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  rep(i, N + 1) cin >> A[i];

  two.resize(N + 1, INF);
  two[0] = 1;
  for (int i = 1; i <= 60; i++)
    two[i] = two[i - 1] * 2;

  vector<ll> b(N + 1, 0);
  b[0] = 1 - A[0];
  if (A[0] >= 2 or (A[0] == 1 && N > 0)) {
    cout << -1 << endl;
    return 0;
  }

  rep(i, N + 1) if (two[i] < A[i]) {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 1; i <= N; i++) {
    if (A[i] > 2 * b[i - 1]) {
      cout << -1 << endl;
      return 0;
    }
    b[i] = min(2 * b[i - 1] - A[i], two[i] - A[i]);
  }

  b[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    chmin(b[i], A[i + 1] + b[i + 1]);
    chmin(b[i], two[i] - A[i]);
  }
  ll ans = 0;
  rep(i, N) ans += b[i];
  rep(i, N + 1) ans += A[i];
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
template <class T> void chmin(T &a, const T &b) {
  if (a > b)
    a = b;
}
template <class T> void chmax(T &a, const T &b) {
  if (a < b)
    a = b;
}

const ll INF = 1e18;
vector<ll> two;

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

  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  rep(i, N + 1) cin >> A[i];

  two.resize(N + 1, INF);
  two[0] = 1;
  for (int i = 1; i <= min(N, 60ll); i++)
    two[i] = two[i - 1] * 2;

  vector<ll> b(N + 1, 0);
  b[0] = 1 - A[0];
  if (A[0] >= 2 or (A[0] == 1 && N > 0)) {
    cout << -1 << endl;
    return 0;
  }

  rep(i, N + 1) if (two[i] < A[i]) {
    cout << -1 << endl;
    return 0;
  }

  for (int i = 1; i <= N; i++) {
    if (A[i] > 2 * b[i - 1]) {
      cout << -1 << endl;
      return 0;
    }
    b[i] = min(2 * b[i - 1] - A[i], two[i] - A[i]);
  }

  b[N] = 0;
  for (int i = N - 1; i >= 0; i--) {
    chmin(b[i], A[i + 1] + b[i + 1]);
    chmin(b[i], two[i] - A[i]);
  }
  ll ans = 0;
  rep(i, N) ans += b[i];
  rep(i, N + 1) ans += A[i];
  cout << ans << endl;

  return 0;
}
Problem p02665 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

B = []

if N == 0:
    if A[0] != 1:
        print(-1)
        exit()
    else:
        print(1)
        exit()

else:
    if A[0] != 0:
        print(-1)
        exit()
    else:
        B.append(1)
        for i in range(1, N + 1):
            B.append((B[i - 1] - A[i - 1]) * 2)
            if A[i] > B[i] or (A[i] == B[i] and i != N):
                print(-1)
                exit()

        ans = 0
        ans += A[N]
        B[N] = A[N]
        for i in range(N, -1, -1):
            ans += min(B[i], B[i + 1] + A[i])
            B[i] = min(B[i], B[i + 1] + A[i])

        print(ans)
        exit()


Fixed submission:
N = int(input())
A = list(map(int, input().split()))

B = []

if N == 0:
    if A[0] != 1:
        print(-1)
        exit()
    else:
        print(1)
        exit()

else:
    if A[0] != 0:
        print(-1)
        exit()
    else:
        B.append(1)
        for i in range(1, N + 1):
            B.append((B[i - 1] - A[i - 1]) * 2)
            if A[i] > B[i] or (A[i] == B[i] and i != N):
                print(-1)
                exit()

        ans = 0
        ans += A[N]
        B[N] = A[N]
        for i in range(N - 1, -1, -1):
            ans += min(B[i], B[i + 1] + A[i])
            B[i] = min(B[i], B[i + 1] + A[i])

        print(ans)
        exit()
Problem p02665 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
a_min = [0] * (n + 1)
a_min[n] = a[n]
for i in range(n - 1, -1, -1):
    a_min[i] = a[i] + a_min[i + 1]

b = [1]
for i in range(n):
    tmp = b[i] - a[i]
    if tmp < 1 or tmp * 2 < a[i + 1]:
        print(-1)
        break
else:
    if n == 0:
        ans = 1 if a[0] == 1 else -1
        print(ans)
    else:
        print(sum(b))


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
a_min = [0] * (n + 1)
a_min[n] = a[n]
for i in range(n - 1, -1, -1):
    a_min[i] = a[i] + a_min[i + 1]

b = [1]
for i in range(n):
    tmp = b[i] - a[i]
    b.append(min(tmp * 2, a_min[i + 1]))
    if tmp < 1 or tmp * 2 < a[i + 1]:
        print(-1)
        break
else:
    if n == 0:
        ans = 1 if a[0] == 1 else -1
        print(ans)
    else:
        print(sum(b))
Problem p02665 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#define MAX 100000
#define ll unsigned long long
using namespace std;

int main() {
  ll n, a[MAX + 100], node[MAX + 100], res = 0;

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

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

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

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

    node[i] = 2 * node[i - 1] - a[i];
  }
  node[n] = 0;

  for (ll i = n; i >= 0; --i) {
    node[i - 1] = node[i] + a[i] >= node[i - 1] ? node[i - 1] : node[i] + a[i];
    res += node[i] + a[i];
  }

  cout << res << endl;
}

Fixed submission:
#include <iostream>
#define MAX 100000
#define ll unsigned long long
using namespace std;

int main() {
  ll n, a[MAX + 100], node[MAX + 100], res = 0;

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

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

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

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

    node[i] = 2 * node[i - 1] - a[i];
  }
  node[n] = 0;

  for (ll i = n + 1; i-- > 0;) {
    node[i - 1] = node[i] + a[i] >= node[i - 1] ? node[i - 1] : node[i] + a[i];
    res += node[i] + a[i];
  }

  cout << res << endl;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n;
  cin >> n;
  bool b = 0;
  v1li v(n + 1), u(n + 1, 1);
  rep(i, n + 1) cin >> v[i];
  lint a = 0, tmp = 0, tt = 0;
  rep(i, n + 1) {
    if (u[i] < v[i]) {
      b = 1;
      break;
    }
    u[i + 1] = (u[i] - v[i]) * 2;
    if (u[i + 2] > 500000000)
      u[i + 2] = 500000000;
  }
  rrep(i, n + 1) {
    tt += v[i];
    tmp = min(tt, u[i]);
    a += tmp;
    tt = tmp;
  }
  if (n == 0)
    a = v[0];
  if (b)
    a = -1;
  cout << a << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define fi first
#define se second
using namespace std;
using lint = long long;
using uint = unsigned int;
using ulint = unsigned long long;
using ldouble = long double;
using pii = pair<int, int>;
using pli = pair<lint, lint>;
using pdd = pair<double, double>;
using pld = pair<ldouble, ldouble>;
using v1i = vector<int>;
using v1li = vector<lint>;
using v2i = vector<vector<int>>;
using v2li = vector<vector<lint>>;
using v3i = vector<vector<vector<int>>>;
using v3li = vector<vector<vector<lint>>>;
using v1b = vector<bool>;
using v2b = vector<vector<bool>>;
using v3b = vector<vector<vector<bool>>>;
using v1c = vector<char>;
using v2c = vector<vector<char>>;
using v3c = vector<vector<vector<char>>>;
constexpr lint mod1 = 1e9 + 7;
constexpr lint mod2 = 998244353;

int main() {
  int n;
  cin >> n;
  bool b = 0;
  v1li v(n + 1), u(n + 1, 1);
  rep(i, n + 1) cin >> v[i];
  lint a = 0, tmp = 0, tt = 0;
  rep(i, n + 1) {
    if (u[i] < v[i]) {
      b = 1;
      break;
    }
    if (i < n) {
      u[i + 1] = (u[i] - v[i]) * 2;
      if (u[i + 1] > 1e15)
        u[i + 1] = 1e15;
    }
  }
  rrep(i, n + 1) {
    tt += v[i];
    tmp = min(tt, u[i]);
    a += tmp;
    tt = tmp;
  }
  if (n == 0)
    a = v[0];
  if (b)
    a = -1;
  cout << a << "\n";
  return 0;
}
Problem p02665 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;

const int N = (int)1e5 + 7;
int n;
ll a[N];
ll b[N];
ll suf[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    suf[i] = suf[i + 1] + a[i];
  }
  if (n == 0) {
    while (1) {
    }
    return 0;
  }
  if (a[0] != 0) {
    cout << "-1\n";
    return 0;
  }
  b[0] = 1;
  for (int i = 1; i <= n; i++) {
    ll lim = min(2 * b[i - 1] - a[i], suf[i + 1]);
    if (lim < b[i - 1] - a[i] || lim < 0) {
      cout << "-1\n";
      return 0;
    }
    b[i] = lim;
  }
  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    sol += a[i] + b[i];
  }
  cout << sol << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
typedef long double ld;

const int N = (int)1e5 + 7;
int n;
ll a[N];
ll b[N];
ll suf[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = n; i >= 1; i--) {
    suf[i] = suf[i + 1] + a[i];
  }
  if (n == 0) {
    if (a[0] == 1) {
      cout << "1\n";
    } else {
      cout << "-1\n";
    }
    return 0;
  }
  if (a[0] != 0) {
    cout << "-1\n";
    return 0;
  }
  b[0] = 1;
  for (int i = 1; i <= n; i++) {
    ll lim = min(2 * b[i - 1] - a[i], suf[i + 1]);
    if (lim < b[i - 1] - a[i] || lim < 0) {
      cout << "-1\n";
      return 0;
    }
    b[i] = lim;
  }
  ll sol = 0;
  for (int i = 0; i <= n; i++) {
    sol += a[i] + b[i];
  }
  cout << sol << "\n";
  return 0;
}
Problem p02665 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  if (N == 0)
    exit(1);
  vector<long long> A(N + 1), B(N + 1);
  for (int i = 0; i <= N; i++) {
    cin >> A[i];
  }

  long long nodes = 1;

  long long sum = A[N];
  B[N] = 0ll;
  for (int i = N - 1; i >= 0; i--) {
    B[i] = sum;
    sum += A[i];

    if (i <= 30) {
      B[i] = min((1ll << i) - A[i], B[i]);
    }
  }

  nodes = 1ll;
  for (int i = 1; i <= N; i++) {
    B[i] = min(B[i - 1] * 2ll - A[i], B[i]);
    if (B[i] < 0ll) {
      puts("-1");
      return 0;
    }
    nodes += A[i] + B[i];
  }

  cout << nodes << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;
  if (N == 0) {
    int a;
    cin >> a;
    if (a == 1)
      puts("1");
    else
      puts("-1");
    return 0;
  }
  vector<long long> A(N + 1), B(N + 1);
  for (int i = 0; i <= N; i++) {
    cin >> A[i];
  }

  long long nodes = 1;

  long long sum = A[N];
  B[N] = 0ll;
  for (int i = N - 1; i >= 0; i--) {
    B[i] = sum;
    sum += A[i];

    if (i <= 30) {
      B[i] = min((1ll << i) - A[i], B[i]);
    }
  }

  nodes = 1ll;
  for (int i = 1; i <= N; i++) {
    B[i] = min(B[i - 1] * 2ll - A[i], B[i]);
    if (B[i] < 0ll) {
      puts("-1");
      return 0;
    }
    nodes += A[i] + B[i];
  }

  cout << nodes << endl;

  return 0;
}
Problem p02665 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;

int main() {
  int N;
  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;
  }

  vector<ll> max_nodes(N + 1);
  max_nodes[0] = 1;
  for (int i = 1; i <= N; i++) {
    max_nodes[i] = min(ll(1LL << 61), max_nodes[i - 1] * 2);
  }

  vector<ll> l(N + 1), r(N + 1);
  if (A[N] > max_nodes[N]) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = N - 1; i >= 0; i--) {
    l[i] = (l[i + 1] + A[i + 1] + 1) / 2;
    r[i] = min(max_nodes[i], (r[i + 1] + A[i + 1]));
    if (l[i] + A[i] > max_nodes[i] || l > r) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll ans = 1;
  ll vs = 1;
  for (int i = 1; i <= N; i++) {
    vs = min(vs * 2 - A[i], r[i]);
    ans += vs + A[i];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1000000007;

int main() {
  int N;
  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;
  }

  vector<ll> max_nodes(N + 1);
  max_nodes[0] = 1;
  for (int i = 1; i <= N; i++) {
    max_nodes[i] = min(ll(1LL << 61), max_nodes[i - 1] * 2);
  }

  vector<ll> l(N + 1), r(N + 1);
  if (A[N] > max_nodes[N]) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = N - 1; i >= 0; i--) {
    l[i] = (l[i + 1] + A[i + 1] + 1) / 2;
    r[i] = min(max_nodes[i], (r[i + 1] + A[i + 1]));
    if (l[i] + A[i] > max_nodes[i] || l[i] > r[i]) {
      cout << -1 << endl;
      return 0;
    }
  }
  ll ans = 1;
  ll vs = 1;
  for (int i = 1; i <= N; i++) {
    vs = min(vs * 2 - A[i], r[i]);
    ans += vs + A[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p02667 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>

typedef long long LL;
const int MN = 100005;

int N, C0, C1, K;
char T[MN];
int stk[MN], cnt[MN], tp;
LL Ans;

int main() {
  scanf("%s", T + 1), N = strlen(T + 1);
  for (int i = 1; i <= N; ++i)
    ++(T[i] & 1 ? C1 : C0);
  Ans = (LL)(C1 + 1) * (C1 + 1) / 4;
  T[0] = T[N + 1] = '0';
  for (int i = 0; i <= N + 1; ++i) {
    int x = T[i] - '0';
    if (!x) {
      if (tp && !stk[tp])
        ++cnt[tp];
      else
        stk[++tp] = 0, cnt[tp] = 1;
    } else {
      if (tp && stk[tp])
        --tp, Ans += C0;
      else
        stk[++tp] = 1, cnt[tp] = 1;
    }
  }
  --cnt[1], --cnt[tp];
  for (int i = 1, s = 0; 2 * i <= tp; ++i)
    s += cnt[2 * i - 1], Ans += (s + i + 1) / 2 - (i + 1) / 2 + (C0 - s);
  printf("%lld\n", Ans);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>

typedef long long LL;
const int MN = 200005;

int N, C0, C1, K;
char T[MN];
int stk[MN], cnt[MN], tp;
LL Ans;

int main() {
  scanf("%s", T + 1), N = strlen(T + 1);
  for (int i = 1; i <= N; ++i)
    ++(T[i] & 1 ? C1 : C0);
  Ans = (LL)(C1 + 1) * (C1 + 1) / 4;
  T[0] = T[N + 1] = '0';
  for (int i = 0; i <= N + 1; ++i) {
    int x = T[i] - '0';
    if (!x) {
      if (tp && !stk[tp])
        ++cnt[tp];
      else
        stk[++tp] = 0, cnt[tp] = 1;
    } else {
      if (tp && stk[tp])
        --tp, Ans += C0;
      else
        stk[++tp] = 1, cnt[tp] = 1;
    }
  }
  --cnt[1], --cnt[tp];
  for (int i = 1, s = 0; 2 * i <= tp; ++i)
    s += cnt[2 * i - 1], Ans += (s + i + 1) / 2 - (i + 1) / 2 + (C0 - s);
  printf("%lld\n", Ans);
  return 0;
}
Problem p02669 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

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

using ll = long long;
const long long LINF = 1e18;
ll v[3] = {2, 3, 5}, w[3], d, n, t;
map<ll, ll> m;
ll f(ll x) {
  if (!x)
    return LINF;
  if (x == 1)
    return d;
  if (m[x])
    return m[x];
  ll res = LINF;
  if (res / d > x - 10)
    chmin(res, x * d);
  for (int i = 0; i < 3; i++) {
    chmin(res, f((x + v[i] - 1) / v[i] + w[i] + (v[i] - x % v[i]) * d));
    chmin(res, f(x / v[i]) + w[i] + (x % v[i]) * d);
  }
  return m[x] = res;
}

int main() {
  cin >> t;
  while (t--) {
    cin >> n >> w[0] >> w[1] >> w[2] >> d;
    m.clear();
    cout << f(n) << endl;
  }
}


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

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

using ll = long long;
const long long LINF = 1e18;
ll v[3] = {2, 3, 5}, w[3], d, n, t;
map<ll, ll> m;
ll f(ll x) {
  if (!x)
    return LINF;
  if (x == 1)
    return d;
  if (m[x])
    return m[x];
  ll res = LINF;
  if (res / d > x - 10)
    chmin(res, x * d);
  for (int i = 0; i < 3; i++) {
    chmin(res, f((x + v[i] - 1) / v[i]) + w[i] + (v[i] - x % v[i]) * d);
    chmin(res, f(x / v[i]) + w[i] + (x % v[i]) * d);
  }
  return m[x] = res;
}

int main() {
  cin >> t;
  while (t--) {
    cin >> n >> w[0] >> w[1] >> w[2] >> d;
    m.clear();
    cout << f(n) << endl;
  }
}
Problem p02669 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

int k, a, b, c, d;
unordered_map<int, int> cache;

int work(int n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (cache.count(n))
    return cache[n];
  int &ans = cache[n];
  ans = 4e18;
  if (n <= 4e9)
    ans = min(ans, n * d);
  for (int i = -15; i <= 15; i++) {
    int nxtN = n + i;
    if (nxtN < 0)
      continue;
    if (nxtN % 2 == 0 && nxtN / 2 < n)
      ans = min(ans, d * abs(i) + a + work(nxtN / 2));
    if (nxtN % 3 == 0 && nxtN / 3 < n)
      ans = min(ans, d * abs(i) + b + work(nxtN / 3));
    if (nxtN % 5 == 0 && nxtN / 5 < n)
      ans = min(ans, d * abs(i) + c + work(nxtN / 5));
    if (nxtN % 6 == 0 && nxtN / 6 < n)
      ans = min(ans, d * abs(i) + a + b + work(nxtN / 6));
    if (nxtN % 15 == 0 && nxtN / 15 < n)
      ans = min(ans, d * abs(i) + b + c + work(nxtN / 15));
    if (nxtN % 10 == 0 && nxtN / 10 < n)
      ans = min(ans, d * abs(i) + a + c + work(nxtN / 10));
  }
  return ans;
}

int32_t main() {
  IOS;
  int t;
  cin >> t;
  while (t--) {
    cache.clear();
    cin >> k >> a >> b >> c >> d;
    int ans = work(k);
    cout << ans << endl;
  }
  return 0;
}

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

int k, a, b, c, d;
unordered_map<int, int> cache;

int work(int n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  if (cache.count(n))
    return cache[n];
  int &ans = cache[n];
  ans = 4e18;
  if (n <= 4e9)
    ans = min(ans, n * d);
  for (int i = -12; i <= 12; i++) {
    int nxtN = n + i;
    if (nxtN < 0)
      continue;
    if (nxtN % 2 == 0 && nxtN / 2 < n)
      ans = min(ans, d * abs(i) + a + work(nxtN / 2));
    if (nxtN % 3 == 0 && nxtN / 3 < n)
      ans = min(ans, d * abs(i) + b + work(nxtN / 3));
    if (nxtN % 5 == 0 && nxtN / 5 < n)
      ans = min(ans, d * abs(i) + c + work(nxtN / 5));
    if (nxtN % 6 == 0 && nxtN / 6 < n)
      ans = min(ans, d * abs(i) + a + b + work(nxtN / 6));
    if (nxtN % 15 == 0 && nxtN / 15 < n)
      ans = min(ans, d * abs(i) + b + c + work(nxtN / 15));
    if (nxtN % 10 == 0 && nxtN / 10 < n)
      ans = min(ans, d * abs(i) + a + c + work(nxtN / 10));
  }
  return ans;
}

int32_t main() {
  IOS;
  int t;
  cin >> t;
  while (t--) {
    cache.clear();
    cin >> k >> a >> b >> c >> d;
    int ans = work(k);
    cout << ans << endl;
  }
  return 0;
}
Problem p02669 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define INF 1e11
using namespace std;
unordered_map<ll, ll> s;
set<ll> p;
ll n, A, B, C, D;
int t;
ll tr(ll x, ll y) {
  if (INF / (x - y) >= D)
    return D * (x - y);
  return INF;
}
void get(ll x, ll y) {
  if (s[x] == 0) {
    s[x] = y;
    p.insert(x);
  } else
    s[x] = min(s[x], y);
}
int main() {
  cin >> t;
  while (t--) {
    s.clear();
    p.clear();
    cin >> n >> A >> B >> C >> D;
    p.insert(n);
    s[0] = min((A + D) * 70, min((B + D) * 48, (C + D + D) * 30));
    while (!p.empty()) {
      auto g = p.end();
      g--;
      ll x = (*g);
      p.erase(g);
      ll y = s[x];
      if (y > s[0])
        continue;
      get(x / 2, y + min(A + D * (x - (x / 2) * 2), tr(x, x / 2)));
      if (x % 2 != 0)
        get(x / 2 + 1, y + min(A + D, tr(x, x / 2 + 1)));
      if (x % 3 <= 1)
        get(x / 3, y + min(B + D * (x - (x / 3) * 3), tr(x, x / 3)));
      else
        get(x / 3 + 1,
            y + min(B + D * ((x / 3 + 1) * 3 - x), tr(x, x / 3 + 1)));
      if (x % 5 < 3)
        get(x / 5, y + min(C + D * (x - (x / 5) * 5), tr(x, x / 5)));
      else
        get(x / 5 + 1,
            y + min(C + D * ((x / 5 + 1) * 5 - x), tr(x, x / 5 + 1)));
    }
    cout << s[0] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1e11
using namespace std;
unordered_map<ll, ll> s;
set<ll> p;
ll n, A, B, C, D;
int t;
ll tr(ll x, ll y) {
  if (INF / (x - y) >= D)
    return D * (x - y);
  return INF;
}
void get(ll x, ll y) {
  if (s[x] == 0) {
    s[x] = y;
    p.insert(x);
  } else
    s[x] = min(s[x], y);
}
int main() {
  cin >> t;
  while (t--) {
    s.clear();
    p.clear();
    cin >> n >> A >> B >> C >> D;
    p.insert(n);
    if (n == 1) {
      cout << D << endl;
      continue;
    }
    s[0] = min((A + D) * 70, min((B + D) * 48, (C + D + D) * 30));
    while (!p.empty()) {
      auto g = p.end();
      g--;
      ll x = (*g);
      p.erase(g);
      ll y = s[x];
      if (y > s[0])
        continue;
      get(x / 2, y + min(A + D * (x - (x / 2) * 2), tr(x, x / 2)));
      if (x % 2 != 0)
        get(x / 2 + 1, y + min(A + D, tr(x, x / 2 + 1)));
      if (x % 3 <= 1)
        get(x / 3, y + min(B + D * (x - (x / 3) * 3), tr(x, x / 3)));
      else
        get(x / 3 + 1,
            y + min(B + D * ((x / 3 + 1) * 3 - x), tr(x, x / 3 + 1)));
      if (x % 5 < 3)
        get(x / 5, y + min(C + D * (x - (x / 5) * 5), tr(x, x / 5)));
      else
        get(x / 5 + 1,
            y + min(C + D * ((x / 5 + 1) * 5 - x), tr(x, x / 5 + 1)));
    }
    cout << s[0] << endl;
  }
  return 0;
}
Problem p02669 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

map<long long, long long> dp;
long long a, b, c, d;

long long rec(long long n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  auto I = dp.find(n);
  if (I != dp.end())
    return I->second;
  long long best = LLONG_MAX;
  if (n % 2 == 0) {
    best = min(best, a + rec(n / 2));
  }
  if (n % 3 == 0) {
    best = min(best, b + rec(n / 3));
  }
  if (n % 5 == 0) {
    best = min(best, c + rec(n / 5));
  }
  if (LLONG_MAX / d > n) {
    best = min(best, d * n);
  }
  for (int i = -10; i <= 10; i++) {
    long long m = n + i;
    if (m < 0)
      continue;
    if (m % 2 == 0 && m / 2 < n) {
      best = min(best, abs(i) * d + a + rec(m / 2));
    }
    if (m % 3 == 0 && m / 3 < n) {
      best = min(best, abs(i) * d + b + rec(m / 3));
    }
    if (m % 5 == 0 && m / 5 < n) {
      best = min(best, abs(i) * d + c + rec(m / 5));
    }
  }
  dp[n] = best;
  return best;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T-- > 0) {
    long long n;
    cin >> n >> a >> b >> c >> d;
    dp.clear();
    long long ans = rec(n);
    cout << ans << "\n";
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

map<long long, long long> dp;
long long a, b, c, d;

long long rec(long long n) {
  if (n == 0)
    return 0;
  if (n == 1)
    return d;
  auto I = dp.find(n);
  if (I != dp.end())
    return I->second;
  long long best = LLONG_MAX;
  if (n % 2 == 0) {
    best = min(best, a + rec(n / 2));
  }
  if (n % 3 == 0) {
    best = min(best, b + rec(n / 3));
  }
  if (n % 5 == 0) {
    best = min(best, c + rec(n / 5));
  }
  if (LLONG_MAX / d > n) {
    best = min(best, d * n);
  }
  for (int i = -5; i <= 5; i++) {
    long long m = n + i;
    if (m < 0)
      continue;
    if (m % 2 == 0 && m / 2 < n) {
      best = min(best, abs(i) * d + a + rec(m / 2));
    }
    if (m % 3 == 0 && m / 3 < n) {
      best = min(best, abs(i) * d + b + rec(m / 3));
    }
    if (m % 5 == 0 && m / 5 < n) {
      best = min(best, abs(i) * d + c + rec(m / 5));
    }
  }
  dp[n] = best;
  return best;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T-- > 0) {
    long long n;
    cin >> n >> a >> b >> c >> d;
    dp.clear();
    long long ans = rec(n);
    cout << ans << "\n";
  }

  return 0;
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
ll dfs(ll x, ll y) {
  ll z = out[x][y] + occ[x][y];
  for (ll i = 0; i < 4; i++) {
    ll x1 = x + dx[i];
    ll y1 = y + dy[i];
    if (out[x1][y1] > z) {
      out[x1][y1] = z;
      dfs(x1, y1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      out[i][j] = min(min(i - 1, abs(n - i)), min(j - 1, abs(n - j)));
      occ[i][j] = 1;
    }
  }
  ll m = n * n;
  ll pr = 0;
  while (m--) {
    ll id;
    cin >> id;
    ll x = (id + n - 1) / n;
    ll y = id % n;
    if (y == 0) {
      y = n;
    }
    occ[x][y] = 0;
    pr += out[x][y];
    dfs(x, y);
  }
  cout << pr;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod = 998244353;
ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
ll n;
ll out[503][503];
ll occ[503][503];
void dfs(ll x, ll y) {
  ll z = out[x][y] + occ[x][y];
  for (ll i = 0; i < 4; i++) {
    ll x1 = x + dx[i];
    ll y1 = y + dy[i];
    if (out[x1][y1] > z) {
      out[x1][y1] = z;
      dfs(x1, y1);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      out[i][j] = min(min(i - 1, abs(n - i)), min(j - 1, abs(n - j)));
      occ[i][j] = 1;
    }
  }
  ll m = n * n;
  ll pr = 0;
  while (m--) {
    ll id;
    cin >> id;
    ll x = (id + n - 1) / n;
    ll y = id % n;
    if (y == 0) {
      y = n;
    }
    occ[x][y] = 0;
    pr += out[x][y];
    dfs(x, y);
  }
  cout << pr;
}
Problem p02670 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  vector<vector<int>> dist(n, vector<int>(n));
  vector<vector<bool>> occupied(n, vector<bool>(n, true));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
    }
  }
  vector<int> bfs;
  bfs.reserve(n * n);
  const int mx[4] = {0, 1, 0, -1};
  const int my[4] = {1, 0, -1, 0};
  for (int _ = 0; _ < n * n; _++) {
    int cur;
    cin >> cur;
    cur--;
    int sx = cur / n, sy = cur % n;
    occupied[sx][sy] = false;
    dist[sx][sy]--;
    ans += dist[sx][sy];
    bfs.push_back(cur);
    for (int s = 0; s < int(bfs.size()); s++) {
      int v = bfs[s];
      int x = v / n, y = v % n;
      for (int d = 0; d < 4; d++) {
        int nx = x + mx[d], ny = y + my[d];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          int nd = occupied[nx][ny] + dist[x][y];
          if (nd < dist[nx][ny]) {
            dist[nx][ny] = nd;
            bfs.push_back(nx * n + ny);
          }
        }
      }
    }
  }
  cout << ans << '\n';
}


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

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  int ans = 0;
  vector<vector<int>> dist(n, vector<int>(n));
  vector<vector<bool>> occupied(n, vector<bool>(n, true));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      dist[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
    }
  }
  vector<int> bfs;
  bfs.reserve(n * n);
  const int mx[4] = {0, 1, 0, -1};
  const int my[4] = {1, 0, -1, 0};
  for (int _ = 0; _ < n * n; _++) {
    int cur;
    cin >> cur;
    cur--;
    int sx = cur / n, sy = cur % n;
    occupied[sx][sy] = false;
    dist[sx][sy]--;
    ans += dist[sx][sy];
    bfs.clear();
    bfs.push_back(cur);
    for (int s = 0; s < int(bfs.size()); s++) {
      int v = bfs[s];
      int x = v / n, y = v % n;
      for (int d = 0; d < 4; d++) {
        int nx = x + mx[d], ny = y + my[d];
        if (0 <= nx && nx < n && 0 <= ny && ny < n) {
          int nd = occupied[nx][ny] + dist[x][y];
          if (nd < dist[nx][ny]) {
            dist[nx][ny] = nd;
            bfs.push_back(nx * n + ny);
          }
        }
      }
    }
  }
  cout << ans << '\n';
}
Problem p02670 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  const int dx[4] = {1, -1, 0, 0};
  const int dy[4] = {0, 0, 1, -1};
  const int INF = INT32_MAX >> 2;
  vector<bool> k(n, true);
  vector<int> d(n * n, INF);
  rep(i, n * n) {
    int x = i % n;
    int y = i / n;
    chmin(d[i], x);
    chmin(d[i], y);
    chmin(d[i], n - x - 1);
    chmin(d[i], n - y - 1);
  }
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    ans += d[p];
    k[p] = false;
    queue<int> q;
    q.emplace(p);
    while (!q.empty()) {
      int nowp = q.front();
      q.pop();
      int x = nowp % n;
      int y = nowp / n;
      rep(j, 4) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        int np = ny * n + nx;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (d[np] > d[nowp] + k[nowp]) {
          d[np] = d[nowp] + k[nowp];
          q.emplace(np);
        }
      }
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  const int dx[4] = {1, -1, 0, 0};
  const int dy[4] = {0, 0, 1, -1};
  const int INF = INT32_MAX >> 2;
  vector<bool> k(n * n, true);
  vector<int> d(n * n, INF);
  rep(i, n * n) {
    int x = i % n;
    int y = i / n;
    chmin(d[i], x);
    chmin(d[i], y);
    chmin(d[i], n - x - 1);
    chmin(d[i], n - y - 1);
  }
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    ans += d[p];
    k[p] = false;
    queue<int> q;
    q.emplace(p);
    while (!q.empty()) {
      int nowp = q.front();
      q.pop();
      int x = nowp % n;
      int y = nowp / n;
      rep(j, 4) {
        int nx = x + dx[j];
        int ny = y + dy[j];
        int np = ny * n + nx;
        if (nx < 0 || nx >= n || ny < 0 || ny >= n)
          continue;
        if (d[np] > d[nowp] + k[nowp]) {
          d[np] = d[nowp] + k[nowp];
          q.emplace(np);
        }
      }
    }
  }
  cout << ans << endl;
}