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

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int main() {
  int N = 0, ans = 0, a = 0;
  int A[10001] = {0};
  int B[10001] = {0};
  cin >> N;
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N; i++) {
    a = A[i];
    if (A[i] - B[i] < 0) {
      ans += A[i];
      A[i] = 0;
      if (A[i + 1] - (B[i] - a) < 0) {
        ans += A[i + 1];
        A[i + 1] = 0;
      } else {
        A[i + 1] -= B[i] - a;
        ans += B[i] - a;
      }
    } else if (A[i] - B[i] >= 0) {
      A[i] -= B[i];
      ans += B[i];
    }
  }
  /*for (int i = 0; i < N + 1; i++) {
          cout << A[i] << endl;
  }*/
  cout << ans;
}


Fixed submission:
#include <iostream>
using namespace std;
int main() {
  long long int N = 0, ans = 0, a = 0;
  long long int A[100001] = {0};
  long long int B[100000] = {0};
  cin >> N;
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N; i++) {
    a = A[i];
    if (A[i] - B[i] < 0) {
      ans += A[i];
      A[i] = 0;
      if (A[i + 1] - (B[i] - a) < 0) {
        ans += A[i + 1];
        A[i + 1] = 0;
      } else {
        A[i + 1] -= B[i] - a;
        ans += B[i] - a;
      }
    } else if (A[i] - B[i] >= 0) {
      A[i] -= B[i];
      ans += B[i];
    }
  }
  /*for (int i = 0; i < N + 1; i++) {
          cout << A[i] << endl;
  }*/
  cout << ans;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define rep(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define rrep(i, a, b) for (int(i) = int(a); (i) >= int(b); (i)--)

#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "

#define INF 1e9 + 1
#define MOD 1e9 + 7
#define INF64 1e18 + 1

#define F first
#define S second

#define Pii pair<int, int>
#define Pll pair<long long, long long>
#define Piii pair<int, pair<int, int>>
#define Plll pair<long long, pair<long long, long long>>

#define Vll(a, b, c)    vector<vector<long long>> (a)((b),vector<long long>((c))
#define Vlll(a, b, c, d) vector<vector<vector<long long>>> (a)((b),vector<vector<long long>>((c),vector<long long>((d)))

#define MAX_N 1000000

int main(void) {
  int N;
  cin >> N;
  vector<long long int> A(N);
  vector<long long int> B(N);
  rep(i, 0, N + 1) { cin >> A[i]; }
  rep(i, 0, N) { cin >> B[i]; }

  long long int ans = 0;

  rep(i, 0, N) {
    if (A[i] >= B[i]) {
      ans += B[i];
    } else {
      B[i] -= A[i];
      ans += A[i];
      if (A[i + 1] >= B[i]) {
        A[i + 1] -= B[i];
        ans += B[i];
      } else {
        ans += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }

  put(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ABS(a) ((a) > (0) ? (a) : -(a))
#define rep(i, a, b) for (int(i) = int(a); (i) < int(b); (i)++)
#define rrep(i, a, b) for (int(i) = int(a); (i) >= int(b); (i)--)

#define put(a) cout << (a) << endl
#define puts(a) cout << (a) << " "

#define INF 1e9 + 1
#define MOD 1e9 + 7
#define INF64 1e18 + 1

#define F first
#define S second

#define Pii pair<int, int>
#define Pll pair<long long, long long>
#define Piii pair<int, pair<int, int>>
#define Plll pair<long long, pair<long long, long long>>

#define Vll(a, b, c)    vector<vector<long long>> (a)((b),vector<long long>((c))
#define Vlll(a, b, c, d) vector<vector<vector<long long>>> (a)((b),vector<vector<long long>>((c),vector<long long>((d)))

#define MAX_N 1000000

int main(void) {
  int N;
  cin >> N;
  vector<long long int> A(N + 1);
  vector<long long int> B(N);
  rep(i, 0, N + 1) { cin >> A[i]; }
  rep(i, 0, N) { cin >> B[i]; }

  long long int ans = 0;

  rep(i, 0, N) {
    if (A[i] >= B[i]) {
      ans += B[i];
    } else {
      B[i] -= A[i];
      ans += A[i];
      if (A[i + 1] >= B[i]) {
        A[i + 1] -= B[i];
        ans += B[i];
      } else {
        ans += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }

  put(ans);
  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()

int main(void) {
  ll N;
  ll ans;

  cin >> N;

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

  ans = 0;
  if (A[0] - B[0] > 0) {
    ans += B[0];
    A[0] -= B[0];
    B[0] = 0;
  } else {
    ans += A[0];
    B[0] -= A[0];
    A[0] = 0;
  }
  if (A[1] - B[0] > 0) {
    ans += B[0];
    A[1] -= B[0];
    B[0] = 0;
  } else {
    ans += A[1];
    B[0] -= A[1];
    A[1] = 0;
  }

  rep2(i, 1, N + 1) {
    if (A[i] - B[i] > 0) {
      ans += B[i];
      A[i] -= B[i];
      B[i] = 0;
    } else {
      ans += A[i];
      B[i] -= A[i];
      A[i] = 0;
    }
    if (A[i + 1] - B[i] > 0) {
      ans += B[i];
      A[i + 1] -= B[i];
      B[i] = 0;
    } else {
      ans += A[i + 1];
      B[i] -= A[i + 1];
      A[i + 1] = 0;
    }
  }

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()

int main(void) {
  ll N;
  ll ans;

  cin >> N;

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

  ans = 0;
  if (A[0] - B[0] > 0) {
    ans += B[0];
    A[0] -= B[0];
    B[0] = 0;
  } else {
    ans += A[0];
    B[0] -= A[0];
    A[0] = 0;
  }
  if (A[1] - B[0] > 0) {
    ans += B[0];
    A[1] -= B[0];
    B[0] = 0;
  } else {
    ans += A[1];
    B[0] -= A[1];
    A[1] = 0;
  }

  rep2(i, 1, N) {
    if (A[i] - B[i] > 0) {
      ans += B[i];
      A[i] -= B[i];
      B[i] = 0;
    } else {
      ans += A[i];
      B[i] -= A[i];
      A[i] = 0;
    }
    if (A[i + 1] - B[i] > 0) {
      ans += B[i];
      A[i + 1] -= B[i];
      B[i] = 0;
    } else {
      ans += A[i + 1];
      B[i] -= A[i + 1];
      A[i + 1] = 0;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

int main() {

  size_t n;
  size_t cnt = 0;

  std::cin >> n;

  std::vector<size_t> a(n + 1), b(n);

  for (auto &&e : a) {
    std::cin >> e;
  }

  for (auto &&e : b) {
    std::cin >> e;
  }

  for (size_t i = 0; i < n; i++) {
    if (a[i] >= b[i]) {
      cnt += b[i];
    } else {
      cnt += a[i];
      b[i] -= a[i];

      if (a[i + 1] >= b[i]) {
        a[i + 1] -= b[i];
        cnt += b[i];
      } else {
        cnt += a[i + i];
        a[i + 1] = 0;
      }
    }
  }

  std::cout << cnt << std::endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

int main() {

  size_t n;
  size_t cnt = 0;

  std::cin >> n;

  std::vector<size_t> a(n + 1), b(n);

  for (auto &&e : a) {
    std::cin >> e;
  }

  for (auto &&e : b) {
    std::cin >> e;
  }

  for (size_t i = 0; i < n; i++) {
    if (a[i] >= b[i]) {
      cnt += b[i];
    } else {
      cnt += a[i];
      b[i] -= a[i];

      if (a[i + 1] >= b[i]) {
        a[i + 1] -= b[i];
        cnt += b[i];
      } else {
        cnt += a[i + 1];
        a[i + 1] = 0;
      }
    }
  }

  std::cout << cnt << std::endl;

  return 0;
}
Problem p02959 (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;
using P = pair<int, int>;

int main() {
  ll N;
  cin >> N;
  vector<ll> B(N + 1);
  vector<ll> A(N);

  ll ans = 0;
  rep(i, N + 1) cin >> A[i];
  rep(i, N) cin >> B[i];

  for (int i = 0; i < N; i++) {
    if (A[i] >= B[i])
      ans += B[i]; // B[i]だけ倒して町にはモンスターが残る
    else if (A[i] < B[i]) {
      ans += A[i];  // 町にいるモンスターは全滅
      B[i] -= A[i]; // 倒せる残りの数を更新
      if (A[i + 1] >= B[i]) { // 次の街にいるモンスターのほうがキャパより多い
        ans += B[i];
        A[i + 1] -= B[i];
      } else if (A[i + 1] < B[i]) { // 次の街も全滅させられる
        ans += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }
  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;
using P = pair<int, int>;

int main() {
  ll N;
  cin >> N;
  vector<ll> B(N + 2);
  vector<ll> A(N + 1);

  ll ans = 0;
  rep(i, N + 1) cin >> A[i];
  rep(i, N) cin >> B[i];

  for (int i = 0; i < N; i++) {
    if (A[i] >= B[i])
      ans += B[i]; // B[i]だけ倒して町にはモンスターが残る
    else if (A[i] < B[i]) {
      ans += A[i];  // 町にいるモンスターは全滅
      B[i] -= A[i]; // 倒せる残りの数を更新
      if (A[i + 1] >= B[i]) { // 次の街にいるモンスターのほうがキャパより多い
        ans += B[i];
        A[i + 1] -= B[i];
      } else if (A[i + 1] < B[i]) { // 次の街も全滅させられる
        ans += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }
  cout << ans << endl;
}
Problem p02959 (C++)

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

using namespace std;

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

  for (int i = 0; i < N; i++) {
    if (B[i] <= A[i]) {
      z = z + B[i];
    } else {
      z = z + A[i];
      if (A[i + 1] <= (B[i] - A[i])) {
        z = z + A[i + 1];
        A[i + 1] = 0;
      } else {
        z = z + (B[i] - A[i]);
        A[i + 1] = A[i + 1] - (B[i] - A[i]);
      }
    }
  }

  cout << z;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

long long N, A[100100], B[100010], z = 0;
int main() {
  cin >> N;
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  for (int i = 0; i < N; i++) {
    if (B[i] <= A[i]) {
      z = z + B[i];
    } else {
      z = z + A[i];
      if (A[i + 1] <= (B[i] - A[i])) {
        z = z + A[i + 1];
        A[i + 1] = 0;
      } else {
        z = z + (B[i] - A[i]);
        A[i + 1] = A[i + 1] - (B[i] - A[i]);
      }
    }
  }

  cout << z;

  return 0;
}
Problem p02959 (C++)

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

int main(void) {

  int N;
  cin >> N;
  vector<long> A(N + 1), B(N);

  for (int i = 0; i < N + 1; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < N; ++i) {
    cin >> B[i];
  }
  B[N] = 0;

  long sum = 0, nokori = 0, nokori_teki = 0, tmp = 0;
  for (int i = 0; i < N + 1; ++i) {
    // 前の残りで全て倒せるとき
    if (nokori >= A[i]) {
      sum += A[i];
      nokori = B[i];
    } // 前の残りと今ので倒せるとき
    else if (B[i] + nokori >= A[i]) {
      sum += A[i];
      nokori = B[i] + nokori - A[i];
    } // 倒しきれないとき
    else {
      sum += nokori + B[i];
      nokori = 0;
    }
  }

  cout << sum << endl;

  return 0;
}

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

int main(void) {

  int N;
  cin >> N;
  vector<long> A(N + 1), B(N + 1);

  for (int i = 0; i < N + 1; ++i) {
    cin >> A[i];
  }
  for (int i = 0; i < N; ++i) {
    cin >> B[i];
  }
  B[N] = 0;

  long sum = 0, nokori = 0, nokori_teki = 0, tmp = 0;
  for (int i = 0; i < N + 1; ++i) {
    // 前の残りで全て倒せるとき
    if (nokori >= A[i]) {
      sum += A[i];
      nokori = B[i];
    } // 前の残りと今ので倒せるとき
    else if (B[i] + nokori >= A[i]) {
      sum += A[i];
      nokori = B[i] + nokori - A[i];
    } // 倒しきれないとき
    else {
      sum += nokori + B[i];
      nokori = 0;
    }
  }

  cout << sum << endl;

  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;

int main(void) {
  long long int n, a[20000], b;
  long long int i, c, f, sum = 0;

  cin >> n;
  for (i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b;
    c = a[i] + a[i + 1];
    f = c - b;
    if (f <= 0) {
      sum += a[i] + a[i + 1];
      a[i + 1] = 0;
    } else if (a[i] >= b) {
      sum += b;
    } else {
      sum += b;
      a[i + 1] = f;
    }
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <math.h>

using namespace std;

int main(void) {
  long long int n, a[200000], b;
  long long int i, c, f, sum = 0;

  cin >> n;
  for (i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  for (i = 0; i < n; i++) {
    cin >> b;
    c = a[i] + a[i + 1];
    f = c - b;
    if (f <= 0) {
      sum += a[i] + a[i + 1];
      a[i + 1] = 0;
    } else if (a[i] >= b) {
      sum += b;
    } else {
      sum += b;
      a[i + 1] = f;
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

#define MIN(X, Y) (((X) > (Y)) ? (Y) : (X))

int main() {
  int N, num = 0;
  vector<int> A;
  cin >> N;
  for (int i = 0; i < N + 1; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++) {
    int b1;
    cin >> b1;
    int left = MIN(A[i], b1);
    num += left;
    b1 -= left;
    int right = MIN(A[i + 1], b1);
    num += right;
    A[i + 1] -= right;
  }
  cout << num << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

#define MIN(X, Y) (((X) > (Y)) ? (Y) : (X))

int main() {
  int N;
  long long num = 0;
  int A[100001];
  cin >> N;
  for (int i = 0; i < N + 1; i++)
    cin >> A[i];
  for (int i = 0; i < N; i++) {
    int b1;
    cin >> b1;
    int left = MIN(A[i], b1);
    num += left;
    b1 -= left;
    int right = MIN(A[i + 1], b1);
    num += right;
    A[i + 1] -= right;
  }
  cout << num << endl;
  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
#define rep(i, n) for (int i = 0; i < n; i++)

int main(void) {
  int N;
  cin >> N;
  vi A(N), B(N - 1);

  rep(i, N + 1) { cin >> A[i]; }
  rep(i, N) { cin >> B[i]; }

  long count = 0;
  rep(i, N) {
    // 正面撃破
    // モンスターの方が多い場合
    if (A[i] >= B[i]) {
      count += B[i];
      A[i] -= B[i];
      B[i] = 0;
      continue;
    }
    // 勇者の方が多い場合
    else if (A[i] < B[i]) {
      count += A[i];
      B[i] -= A[i];
      A[i] = 0;
    }

    // 右隣撃破
    // モンスターの方が多い場合
    if (A[i + 1] >= B[i]) {
      count += B[i];
      A[i + 1] -= B[i];
      B[i] = 0;
      continue;
    }
    // 勇者の方が多い場合
    else if (A[i] < B[i]) {
      count += A[i + 1];
      B[i] -= A[i + 1];
      A[i + 1] = 0;
    }
  }

  cout << count << endl;

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
#define rep(i, n) for (int i = 0; i < n; i++)

int main(void) {
  int N;
  cin >> N;
  vi A(N + 1), B(N);

  rep(i, N + 1) { cin >> A[i]; }
  rep(i, N) { cin >> B[i]; }

  long count = 0;
  rep(i, N) {
    // 正面撃破
    // モンスターの方が多い場合
    if (A[i] >= B[i]) {
      count += B[i];
      A[i] -= B[i];
      B[i] = 0;
      continue;
    }
    // 勇者の方が多い場合
    else if (A[i] < B[i]) {
      count += A[i];
      B[i] -= A[i];
      A[i] = 0;
    }

    // 右隣撃破
    // モンスターの方が多い場合
    if (A[i + 1] >= B[i]) {
      count += B[i];
      A[i + 1] -= B[i];
      B[i] = 0;
      continue;
    }
    // 勇者の方が多い場合
    else if (A[i] < B[i]) {
      count += A[i + 1];
      B[i] -= A[i + 1];
      A[i + 1] = 0;
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
int N;

ll sum_ll(vector<int> vec) {
  ll _sum = 0;
  rep(i, (int)vec.size()) _sum += vec[i];
  return _sum;
}

void beat(int &_hero, int &_enemy) {
  while (_hero > 0 && _enemy > 0) {
    _hero--;
    _enemy--;
  }
}

int main() {
  cin >> N;
  vector<int> hero(N);
  vector<int> enemy(N + 1);
  rep(i, N + 1) cin >> enemy[i];
  rep(i, N) cin >> hero[i];

  ll ans = sum_ll(enemy);
  rep(i, N) {
    beat(hero[i], enemy[i]);
    beat(hero[i], enemy[i + 1]);
  }
  cout << ans - sum_ll(enemy) << "\n";
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
int N;

ll sum_ll(vector<int> vec) {
  ll _sum = 0;
  rep(i, (int)vec.size()) _sum += vec[i];
  return _sum;
}

void beat(int &_hero, int &_enemy) {
  if (_hero < _enemy) {
    _enemy -= _hero;
    _hero = 0;
  } else {
    _hero -= _enemy;
    _enemy = 0;
  }
}

int main() {
  cin >> N;
  vector<int> hero(N);
  vector<int> enemy(N + 1);
  rep(i, N + 1) cin >> enemy[i];
  rep(i, N) cin >> hero[i];

  ll ans = sum_ll(enemy);
  rep(i, N) {
    beat(hero[i], enemy[i]);
    beat(hero[i], enemy[i + 1]);
  }
  cout << ans - sum_ll(enemy) << "\n";
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int A[20000];
  int B[20000];
  cin >> n;
  for (int i = 0; i < n + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> B[i];
  }

  long long ans = 0;
  int del;
  int sub;
  for (int i = 0; i < n; i++) {
    del = min(A[i], B[i]);
    A[i] -= del;
    ans += del;
    del = min(A[i + 1], B[i] - del);
    A[i + 1] -= del;
    ans += del;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int A[200000];
  int B[200000];
  cin >> n;
  for (int i = 0; i < n + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> B[i];
  }

  long long ans = 0;
  int del;
  int sub;
  for (int i = 0; i < n; i++) {
    del = min(A[i], B[i]);
    A[i] -= del;
    ans += del;
    del = min(A[i + 1], B[i] - del);
    A[i + 1] -= del;
    ans += del;
  }
  cout << ans << endl;
}
Problem p02959 (C++)

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

#define int long long

int n, a[10005], b[10005], ans = 0;

signed main() {
  cin >> n;
  for (int i = 1; i <= n + 1; cin >> a[i++])
    ;
  for (int i = 1; i <= n; cin >> b[i++])
    ;
  for (int i = 1; i <= n; i++) {
    if (b[i] >= a[i]) {
      ans += min(b[i], a[i] + a[i + 1]);
      a[i + 1] = max(a[0], a[i + 1] - b[i] + a[i]);
    } else
      ans += b[i];
  }
  cout << ans << endl;

  return 0;
}

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

#define int long long

int n, a[100005], b[100005], ans = 0;

signed main() {
  cin >> n;
  for (int i = 1; i <= n + 1; cin >> a[i++])
    ;
  for (int i = 1; i <= n; cin >> b[i++])
    ;
  for (int i = 1; i <= n; i++) {
    if (b[i] >= a[i]) {
      ans += min(b[i], a[i] + a[i + 1]);
      a[i + 1] = max(a[0], a[i + 1] - b[i] + a[i]);
    } else
      ans += b[i];
  }
  cout << ans << endl;

  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
/****************************************************
 *   Template for coding contests                    *
 *   Author    :    Sanjeev Sharma                   *
 *   Email     :    thedevelopersanjeev@gmail.com    *
 *****************************************************/
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> a(n + 1), b(n);
  for (long long i = 0; i < n + 1; i++)
    cin >> b[i];
  for (long long i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    // ith hero will first finish monsters in ith position
    // and then move to (i+1)th position
    long long temp = min(a[i], b[i]);
    // kill monsters at ith position
    a[i] -= temp;
    b[i] -= temp;
    ans += temp;
    // kill monsters at (i+1)th position
    temp = min(a[i], b[i + 1]);
    a[i] -= temp;
    b[i + 1] -= temp;
    ans += temp;
  }
  cout << ans;
  return 0;
}


Fixed submission:
/****************************************************
 *   Template for coding contests                    *
 *   Author    :    Sanjeev Sharma                   *
 *   Email     :    thedevelopersanjeev@gmail.com    *
 *****************************************************/
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  vector<long long> b(n + 1), a(n);
  for (long long i = 0; i < n + 1; i++)
    cin >> b[i];
  for (long long i = 0; i < n; i++)
    cin >> a[i];
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    // ith hero will first finish monsters in ith position
    // and then move to (i+1)th position
    long long temp = min(a[i], b[i]);
    // kill monsters at ith position
    a[i] -= temp;
    b[i] -= temp;
    ans += temp;
    // kill monsters at (i+1)th position
    temp = min(a[i], b[i + 1]);
    a[i] -= temp;
    b[i + 1] -= temp;
    ans += temp;
  }
  cout << ans;
  return 0;
}
Problem p02959 (C++)

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

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int N;
  cin >> N;
  vector<ll> A(N + 1);
  REP(i, N + 1) { cin >> A.at(i); }
  vector<ll> B(N);
  REP(i, N) { cin >> B.at(i); }

  ll ans = 0;
  REP(i, N) {
    ans += min(A.at(i), B.at(i));
    A.at(i) -= min(A.at(i), B.at(i));
    ans += min(B.at(i + 1) - A.at(i), B.at(i + 1));
    B.at(i) -= min(B.at(i + 1) - A.at(i), B.at(i + 1));
  }

  cout << ans << endl;

  return 0;
}


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

#define REP(i, x) REPI(i, 0, x)
#define REPI(i, a, b) for (int i = int(a); i < int(b); ++i)
#define ALL(x) (x).begin(), (x).end()

typedef long long ll;
using namespace std;

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

  int N;
  cin >> N;
  vector<ll> A(N + 1);
  REP(i, N + 1) { cin >> A.at(i); }
  vector<ll> B(N);
  REP(i, N) { cin >> B.at(i); }

  ll ans = 0;
  REP(i, N) {
    ll cnt1, cnt2;
    cnt1 = min(A.at(i), B.at(i));
    ans += cnt1;
    A.at(i) -= cnt1;
    B.at(i) -= cnt1;

    cnt2 = min(A.at(i + 1), B.at(i));
    ans += cnt2;
    A.at(i + 1) -= cnt2;
  }

  cout << ans << endl;

  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int inputValue() {
  int a;
  cin >> a;
  return a;
};

void inputArray(int *p, int a){REP(i, a){cin >> p[i];
}
}
;

void inputVector(vector<int> *p, int a) {
  REP(i, a) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}

int main() {
  int N = inputValue();
  int A[100001];
  int B[100000];
  REP(i, N + 1)
  A[i] = inputValue();
  REP(i, N)
  B[i] = inputValue();
  int c = 0;
  int r = N / 2;
  REP(i, r) {
    while (A[i] > 0 && B[i] > 0) {
      A[i]--;
      B[i]--;
      c++;
    }
    while (A[i + 1] > 0 && B[i] > 0) {
      A[i + 1]--;
      B[i]--;
      c++;
    }
    while (A[N - i] > 0 && B[N - 1 - i] > 0) {
      A[N - i]--;
      B[N - 1 - i]--;
      c++;
    }
    while (A[N - 1 - i] > 0 && B[N - 1 - i] > 0) {
      A[N - 1 - i]--;
      B[N - 1 - i]--;
      c++;
    }
  }
  while (A[r] > 0 && B[r] > 0) {
    A[r]--;
    B[r]--;
    c++;
  }
  while (A[r + 1] > 0 && B[r] > 0) {
    A[r + 1]--;
    B[r]--;
    c++;
  }
  cout << c << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

int inputValue() {
  int a;
  cin >> a;
  return a;
};

void inputArray(int *p, int a){REP(i, a){cin >> p[i];
}
}
;

void inputVector(vector<int> *p, int a) {
  REP(i, a) {
    int input;
    cin >> input;
    p->push_back(input);
  }
}

int main() {
  int N = inputValue();
  int A[100001];
  int B[100000];
  REP(i, N + 1)
  A[i] = inputValue();
  REP(i, N)
  B[i] = inputValue();
  ll c = 0;
  int f1 = 0;
  int f2 = 0;
  REP(i, N) {
    f1 = min(A[i] - f2, B[i]);
    c += f1;
    f2 = min(A[i + 1], B[i] - f1);
    c += f2;
  }
  cout << c << endl;
}
Problem p02959 (C++)

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long n, a[100005], b[100001], ans, zero = 0;
int main() {
  cin >> n;
  for (int i = 1; i <= n + 1; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    if (b[i] > a[i]) {
      ans += min(a[i] + a[i + 1], b[i]);
      a[i + 1] = max(zero, a[i] + a[i + 1] - b[i]);
    } else
      ans += b[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02959 (C++)

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

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define f(i, a, b) for (int i = a; i < b; i++)
#define MOD 1000000007
bool is_int_lround(double x) { return std::lround(x) == x; }
int ketasuu(int x) {
  int n = 0;
  while (x > 0) {
    x /= 10;
    n++;
  }
  return n;
}
ll gcd_ll(ll x, ll y) {
  long long a = max(x, y), b = min(x, y), c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

int abs(int x, int y) {
  if (x >= y) {
    return x - y;
  } else if (y > x) {
    return y - x;
  }
}
ll fiv(ll n) {
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return 1;
  }
  return fiv(n - 1) + fiv(n - 2);
}
ll conbi(int n, int m) {

  cin >> n >> m;
  vector<ll> a(100);
  a[0] = 1;
  for (int i = 0; i < 14; i++) {
    a[i + 1] = a[i] * (i + 1);
  }
  return a[n] / (a[m] * a[n - m]);
}

int main() {
  int n;
  cin >> n;
  vl a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];
  ll ans = 0;
  vl c(n);
  c[0] = 0;
  rep(i, n) {
    c[i + 1] = min(a[i + 1], b[i] - min(a[i] - c[i], b[i]));
    ans += min(a[i] - c[i], b[i]) + c[i + 1];
  }
  cout << ans << endl;
}


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

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;

#define rep(i, n)                                                              \
  ;                                                                            \
  for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define f(i, a, b) for (int i = a; i < b; i++)
#define MOD 1000000007
bool is_int_lround(double x) { return std::lround(x) == x; }
int ketasuu(int x) {
  int n = 0;
  while (x > 0) {
    x /= 10;
    n++;
  }
  return n;
}
ll gcd_ll(ll x, ll y) {
  long long a = max(x, y), b = min(x, y), c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

int abs(int x, int y) {
  if (x >= y) {
    return x - y;
  } else if (y > x) {
    return y - x;
  }
}
ll fiv(ll n) {
  if (n == 0) {
    return 1;
  } else if (n == 1) {
    return 1;
  }
  return fiv(n - 1) + fiv(n - 2);
}
ll conbi(int n, int m) {

  cin >> n >> m;
  vector<ll> a(100);
  a[0] = 1;
  for (int i = 0; i < 14; i++) {
    a[i + 1] = a[i] * (i + 1);
  }
  return a[n] / (a[m] * a[n - m]);
}

int main() {
  int n;
  cin >> n;
  vl a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];
  ll ans = 0;
  vl c(n + 1);
  c[0] = 0;
  rep(i, n) {
    c[i + 1] = min(a[i + 1], b[i] - min(a[i] - c[i], b[i]));
    ans += min(a[i] - c[i], b[i]) + c[i + 1];
  }
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[10005];
  long long ans = 0;
  for (int i = 1; i <= n + 1; ++i)
    cin >> a[i];
  for (int i = 1, b; i <= n; ++i) {
    cin >> b;
    int x = max(0, a[i] - b);
    ans += a[i] - x;
    b -= a[i] - x;
    a[i] = x;
    if (b > 0) {
      int x = max(0, a[i + 1] - b);
      ans += a[i + 1] - x;
      b -= a[i + 1] - x;
      a[i + 1] = x;
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[100005];
  long long ans = 0;
  for (int i = 1; i <= n + 1; ++i)
    cin >> a[i];
  for (int i = 1, b; i <= n; ++i) {
    cin >> b;
    int x = max(0, a[i] - b);
    ans += a[i] - x;
    b -= a[i] - x;
    a[i] = x;
    if (b > 0) {
      int x = max(0, a[i + 1] - b);
      ans += a[i + 1] - x;
      b -= a[i + 1] - x;
      a[i + 1] = x;
    }
  }
  cout << ans;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#define rep(i, b) for (ll i = 0; i < b; i++)
#define ll long long
using namespace std;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr << #x << " " << x << '\n';

int main() {
  ll n, ans = 0;
  cin >> n;
  vl a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n + 1) {
    ans += min(a[i], b[i] + b[i - 1]);
    b[i] = max((ll)0, b[i] - (a[i] - b[i - 1]));
  }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
#define rep(i, b) for (ll i = 0; i < b; i++)
#define ll long long
using namespace std;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr << #x << " " << x << '\n';

int main() {
  ll n, ans = 0;
  cin >> n;
  vl a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) {
    ans += min(b[i], a[i] + a[i + 1]);
    a[i + 1] -= min(b[i], a[i] + a[i + 1]) - min(b[i], a[i]);
  }
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ':' << x << endl
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> A(n, 0LL), B(n, 0LL);
  REP(i, n + 1) cin >> A[i];
  REP(i, n) cin >> B[i];

  ll cnt = 0LL;
  for (int i = 0; i <= n - 1; i++) {
    // cout << "B[i]" << B[i] << endl;
    if (B[i] >= A[i]) {
      cnt += A[i];
      B[i] -= A[i];
      A[i] = 0;
      if (B[i] >= A[i + 1]) {
        cnt += A[i + 1];
        B[i] -= A[i + 1];
        A[i + 1] = 0;
      } else {
        cnt += B[i];
        A[i + 1] -= B[i];
        B[i] = 0;
      }
    } else {
      cnt += B[i];
      A[i] -= B[i];
      B[i] = 0;
    }
    // cout << cnt << endl;
  }
  // REP(i,n+1) cout << A[i] << " ";
  // cout << endl;
  // REP(i,n) cout << B[i] << " ";
  // cout << endl;
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ':' << x << endl
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef long long ll;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> A(n + 1, 0LL), B(n, 0LL);
  REP(i, n + 1) cin >> A[i];
  REP(i, n) cin >> B[i];

  ll cnt = 0LL;
  for (int i = 0; i <= n - 1; i++) {
    // cout << "B[i]" << B[i] << endl;
    if (B[i] >= A[i]) {
      cnt += A[i];
      B[i] -= A[i];
      A[i] = 0;
      if (B[i] >= A[i + 1]) {
        cnt += A[i + 1];
        B[i] -= A[i + 1];
        A[i + 1] = 0;
      } else {
        cnt += B[i];
        A[i + 1] -= B[i];
        B[i] = 0;
      }
    } else {
      cnt += B[i];
      A[i] -= B[i];
      B[i] = 0;
    }
    // cout << cnt << endl;
  }
  // REP(i,n+1) cout << A[i] << " ";
  // cout << endl;
  // REP(i,n) cout << B[i] << " ";
  // cout << endl;
  cout << cnt << endl;
  return 0;
}
Problem p02959 (C++)

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

int main() {
  int n;
  cin >> n;

  vector<int> monster(n), hero(n - 1);
  for (int i = 0; i < n + 1; i++) {
    cin >> monster[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> hero[i];
  }

  ll ans = 0;
  int force;
  for (int i = 0; i < n; i++) {
    force = max(hero[i] - monster[i], 0);
    ans += min(monster[i], hero[i]);
    // cout << ans << endl;
    ans += min(force, monster[i + 1]);
    // cout << ans << endl;
    monster[i + 1] -= min(force, monster[i + 1]);
  }

  cout << ans << endl;
}


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

int main() {
  int n;
  cin >> n;

  vector<int> monster(n + 1), hero(n);
  for (int i = 0; i < n + 1; i++) {
    cin >> monster[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> hero[i];
  }

  ll ans = 0;
  int force;
  for (int i = 0; i < n; i++) {
    force = max(hero[i] - monster[i], 0);
    ans += min(monster[i], hero[i]);
    // cout << ans << endl;
    ans += min(force, monster[i + 1]);
    // cout << ans << endl;
    monster[i + 1] -= min(force, monster[i + 1]);
  }

  cout << ans << endl;
}
Problem p02959 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

s1 = sum(a)
for i in range(n + 1):
    if i == 0:
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
    elif i == n:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
            b[i] = 0
    else:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
print(s1 - sum(a))


Fixed submission:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

s1 = sum(a)
for i in range(n + 1):
    if i == 0:
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
    elif i == n:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
            b[i - 1] = 0
    else:
        if a[i] < b[i - 1]:
            a[i] = 0
        else:
            a[i] -= b[i - 1]
        if a[i] < b[i]:
            b[i] -= a[i]
            a[i] = 0
        else:
            a[i] -= b[i]
            b[i] = 0
print(s1 - sum(a))
Problem p02959 (Python)

Buggy submission (status: Runtime Error):
# coding:utf-8
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0

for i in range(n):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        ans += a[i]
        b[i] -= a[i]
        if a[i + 1] >= b[i]:
            a[i + 1] -= b[i]
            ans += b[i]
        else:
            ans += a[i + i]
            a[i + 1] = 0


print(ans)


Fixed submission:
# coding:utf-8
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

ans = 0

for i in range(n):
    if a[i] >= b[i]:
        ans += b[i]
    else:
        ans += a[i]
        b[i] -= a[i]
        if a[i + 1] >= b[i]:
            a[i + 1] -= b[i]
            ans += b[i]
        else:
            ans += a[i + 1]
            a[i + 1] = 0


print(ans)
Problem p02959 (Python)

Buggy submission (status: Runtime Error):
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[: n + 1], ab[n + 1 :]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        if a[n - i] < _b:
            _b -= a[n - i]
            a[n - i] = 0
            a[n - i - 1] = max(0, a[n - i - 1] - _b)
        else:
            a[n - i] -= _b
    return str(_sum - sum(a))


if __name__ == "__main__":
    n = int(input())
    print(solve("{}\n".format(n) + "\n".join([input() for _ in range(n)])))


Fixed submission:
def solve(string):
    n, *ab = map(int, string.split())
    a, b = ab[: n + 1], ab[n + 1 :]
    _sum = sum(a)
    for i, _b in enumerate(b[::-1]):
        if a[n - i] < _b:
            _b -= a[n - i]
            a[n - i] = 0
            a[n - i - 1] = max(0, a[n - i - 1] - _b)
        else:
            a[n - i] -= _b
    return str(_sum - sum(a))


if __name__ == "__main__":
    n = int(input())
    print(solve("{}\n".format(n) + "\n".join([input() for _ in range(2)])))
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

#define rpt(i, n) for (i = 0; i < (n); i++)
typedef long long ll;

int main(void) {
  ll n, a[10 ^ 5 + 1], b[10 ^ 5], i, j, count = 0;

  cin >> n;
  for (i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  rpt(i, n) { cin >> b[i]; }

  rpt(i, n) {
    if (a[i] >= b[i]) {
      count += b[i];
    } else {
      if (b[i] >= (a[i] + a[i + 1])) {
        count += (a[i] + a[i + 1]);
        a[i + 1] = 0;
      } else {
        count += b[i];
        a[i + 1] -= (b[i] - a[i]);
      }
    }
  }

  printf("%lld", count);

  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;

#define rpt(i, n) for (i = 0; i < (n); i++)
typedef long long ll;

int main(void) {
  ll n, a[100005], b[100000], i, j, count = 0;

  cin >> n;
  for (i = 0; i < n + 1; i++) {
    cin >> a[i];
  }
  rpt(i, n) { cin >> b[i]; }

  rpt(i, n) {
    if (a[i] >= b[i]) {
      count += b[i];
    } else {
      if (b[i] >= (a[i] + a[i + 1])) {
        count += (a[i] + a[i + 1]);
        a[i + 1] = 0;
      } else {
        count += b[i];
        a[i + 1] -= (b[i] - a[i]);
      }
    }
  }

  printf("%lld", count);

  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n);
  ll sum1 = 0;
  rep(i, n + 1) {
    cin >> a[i];
    sum1 += a[i];
  }
  rep(i, n) cin >> b[i];

  rep(i, n) {
    if (b[i] <= a[i])
      a[i] -= b[i];
    else if (b[i] <= a[i] + a[i + 1]) {
      a[i + 1] -= b[i] - a[i];
      a[i] = 0;
    } else {
      a[i] = 0;
      a[i + 1] = 0;
    }
  }

  ll sum2 = 0;
  rep(i, n + 1) sum2 += a[i];
  cout << sum1 - sum2;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n);
  ll sum1 = 0;
  rep(i, n + 1) {
    cin >> a[i];
    sum1 += a[i];
  }
  rep(i, n) cin >> b[i];

  rep(i, n) {
    if (b[i] <= a[i])
      a[i] -= b[i];
    else if (b[i] <= a[i] + a[i + 1]) {
      a[i + 1] -= b[i] - a[i];
      a[i] = 0;
    } else {
      a[i] = 0;
      a[i + 1] = 0;
    }
  }

  ll sum2 = 0;
  rep(i, n + 1) sum2 += a[i];
  cout << sum1 - sum2;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

long long larger(long long a, long long b) {
  if (a >= b) {
    return a;
  } else {
    return b;
  }
}

long long smaller(long long a, long long b) {
  if (a <= b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  // input
  int n;
  cin >> n;
  long long counter = 0;
  vector<long long> a(n + 2, 0);
  vector<long long> b(n + 2, 0);
  for (int i = 1; i < n + 2; i++)
    cin >> a[i];
  for (int i = 1; i < n + 1; i++)
    cin >> b[i];

  // compute
  for (int i = 1; i < n + 2; i++) {
    counter += smaller(a[i] + a[i + 1], b[i]);
    a[i + 1] = smaller(0, a[i + 1] - larger(0, b[i] - a[i]));
  }

  // output
  cout << counter << endl;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;

long long larger(long long a, long long b) {
  if (a >= b) {
    return a;
  } else {
    return b;
  }
}

long long smaller(long long a, long long b) {
  if (a <= b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  // input
  int n;
  cin >> n;
  long long counter = 0;
  vector<long long> a(n + 2, 0);
  vector<long long> b(n + 2, 0);
  for (int i = 1; i < n + 2; i++)
    cin >> a[i];
  for (int i = 1; i < n + 1; i++)
    cin >> b[i];

  // compute
  for (int i = 1; i < n + 2; i++) {
    counter += smaller(a[i] + a[i + 1], b[i]);
    a[i + 1] = larger(0, a[i + 1] - larger(0, b[i] - a[i]));
  }

  // output
  cout << counter << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N + 1), B(N + 1);
  rep(i, N + 1) cin >> A[i];
  rep(i, N) cin >> B[i];
  B[N + 1] = 0;

  long long sum = 0;
  rep(i, N) sum += B[i];

  int a = 0, b;
  rep(i, N + 1) {
    b = A[i] - a;
    if (b < 0)
      sum += b, b = 0;
    a = B[i] - b;
    if (a < 0)
      a = 0;
  }
  cout << sum;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int N;
  cin >> N;
  int A[100001], B[100001];
  rep(i, N + 1) cin >> A[i];
  rep(i, N) cin >> B[i];
  B[N + 1] = 0;

  long long sum = 0;
  rep(i, N) sum += B[i];

  int a = 0, b;
  rep(i, N + 1) {
    b = A[i] - a;
    if (b < 0)
      sum += b, b = 0;
    a = B[i] - b;
    if (a < 0)
      a = 0;
  }
  cout << sum;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[10005], b[10005], sum;
signed main() {
  cin >> n;
  for (int i = 1; i <= n + 1; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  for (int i = 1; i <= n; i++) {
    if (b[i] < a[i]) {
      a[i] -= b[i], sum += b[i];
      continue;
    }
    if (b[i] >= a[i])
      b[i] -= a[i], sum += a[i], a[i] = 0;
    if (b[i] >= a[i + 1])
      sum += a[i + 1], a[i + 1] = 0;
    else
      sum += b[i], a[i + 1] -= b[i];
  }
  cout << sum;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, a[100005], b[100005], sum;
signed main() {
  cin >> n;
  for (int i = 1; i <= n + 1; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  for (int i = 1; i <= n; i++) {
    if (b[i] < a[i]) {
      a[i] -= b[i], sum += b[i];
      continue;
    }
    if (b[i] >= a[i])
      b[i] -= a[i], sum += a[i], a[i] = 0;
    if (b[i] >= a[i + 1])
      sum += a[i + 1], a[i + 1] = 0;
    else
      sum += b[i], a[i + 1] -= b[i];
  }
  cout << sum;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long int ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  vector<ll> B(N);
  for (ll i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> B[i];
  }
  ll Count = 0;
  for (ll i = 0; i < N; i++) {
    if (A[i] >= B[i]) {
      Count += B[i];
      B[i] = 0;
    } else {
      B[i] -= A[i];
      Count += A[i];
      if (A[i + 1] >= B[i]) {
        A[i + 1] -= B[i];
        Count += B[i];
      } else {
        Count += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }
  if (A[N] >= B[N]) {
    B[N] = 0;
    Count += B[N];
  } else
    Count += A[N];
  cout << Count;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long int ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  vector<ll> B(N);
  for (ll i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> B[i];
  }
  ll Count = 0;
  for (ll i = 0; i < N; i++) {
    if (A[i] >= B[i]) {
      Count += B[i];
      B[i] = 0;
    } else {
      B[i] -= A[i];
      Count += A[i];
      if (A[i + 1] >= B[i]) {
        A[i + 1] -= B[i];
        Count += B[i];
      } else {
        Count += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }
  cout << Count;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> cities(n + 1);
  vector<long long> heros(n + 1);

  for (int i = 0; i < n + 1; i++) {
    cin >> cities[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> heros[i];
  }
  heros[n + 1] = 0;

  long long acc = 0;
  long long prev_hero = 0;
  long long prev_hero_min = 0;
  for (int i = 0; i < n + 1; i++) {
    acc += min((prev_hero + heros[i]), cities[i]);
    prev_hero = max(heros[i] - max((cities[i] - prev_hero), prev_hero_min),
                    prev_hero_min);
  }

  cout << acc << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> cities(n + 1);
  vector<long long> heros(n + 1);

  for (int i = 0; i < n + 1; i++) {
    cin >> cities[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> heros[i];
  }
  heros[n] = 0;

  long long acc = 0;
  long long prev_hero = 0;
  long long prev_hero_min = 0;
  for (int i = 0; i < n + 1; i++) {
    acc += min((prev_hero + heros[i]), cities[i]);
    prev_hero = max(heros[i] - max((cities[i] - prev_hero), prev_hero_min),
                    prev_hero_min);
  }

  cout << acc << endl;
  return 0;
}