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

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

const long long INF = 1000000000;

typedef long long ll;

#define writeln(n) cout << n << "\n";

typedef pair<int, int> P;
typedef pair<string, int> Psi;

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

  int n;
  cin >> n;

  vector<int> d(n);

  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (d[i] != i)
      cnt++;
  }

  if (cnt == 0 || cnt == 2) {
    writeln("YES");
  } else {
    writeln("NO");
  }
}


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

const long long INF = 1000000000;

typedef long long ll;

#define writeln(n) cout << n << "\n";

typedef pair<int, int> P;
typedef pair<string, int> Psi;

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

  int n;
  cin >> n;

  vector<int> d(n + 1);

  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (d[i] != i)
      cnt++;
  }

  if (cnt == 0 || cnt == 2) {
    writeln("YES");
  } else {
    writeln("NO");
  }
}
Problem p02958 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n;
LL a[500 + 5], b[500 + 5];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  int now[2], nowl = 0;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i]) {
      if (nowl == 2) {
        cout << "NO\n";
        return 0;
      }
      now[nowl++] = i;
    }
  if (a[now[0]] == b[now[1]] && a[now[1]] == b[now[0]])
    printf("YES\n");
  else
    printf("NO\n");
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
int n;
LL a[500 + 5], b[500 + 5];
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i];
  }
  sort(b, b + n);
  int now[2], nowl = 0;
  for (int i = 0; i < n; i++)
    if (a[i] != b[i]) {
      if (nowl == 2) {
        cout << "NO\n";
        return 0;
      }
      now[nowl++] = i;
    }

  if (nowl == 0 ||
      (nowl == 2 && a[now[0]] == b[now[1]] && a[now[1]] == b[now[0]]))
    printf("YES\n");
  else
    printf("NO\n");
}
Problem p02958 (C++)

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

int main() {

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; i < n; j++) {
      swap(p[i], p[j]);
      if (p == q) {
        cout << "YES" << endl;
        return 0;
      } else
        swap(p[i], p[j]);
    }
  }

  cout << "NO" << endl;

  return 0;
}

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

int main() {

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      swap(p[i], p[j]);
      if (p == q) {
        cout << "YES" << endl;
        return 0;
      } else
        swap(p[i], p[j]);
    }
  }

  cout << "NO" << endl;

  return 0;
}
Problem p02958 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> number(N);
  for (int i = 0; i < N; i++) {
    cin >> number.at(i);
  }
  int count = 0;

  for (int i = 0; i < N; i++) {
    if (number.at(i) + 1 != number.at(i + 1)) {
      count++;
    }
  }
  if (count == 2 || count == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}


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

int main() {
  int N;
  cin >> N;
  vector<int> number(N);
  for (int i = 0; i < N; i++) {
    cin >> number.at(i);
  }
  int count = 0;

  for (int i = 0; i < N; i++) {
    if (number.at(i) != i + 1) {
      count++;
    }
  }
  if (count == 2 || count == 0) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
Problem p02958 (C++)

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

using namespace std;

#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;

#define INF INT_MAX
#define MOD 1000000007
#define PI acos(-1)

using ll = long long;
using ull = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;

  vector<ll> p(N);
  rep(i, N) { cin >> p[i]; }

  rep(k, N - 1) {
    if (p[k] > p[k + 1]) {
      break;
    }
    if (k == N - 2) {
      YES;
      return 0;
    }
  }

  rep(i, N) {
    for (ll j = i + 1; j <= N; j++) {
      vector<ll> vec(N);
      copy(p.begin(), p.end(), vec.begin());
      ll tmp = vec[i];
      vec[i] = vec[j];
      vec[j] = tmp;
      rep(k, N - 1) {
        if (vec[k] > vec[k + 1]) {
          break;
        }
        if (k == N - 2) {
          YES;
          return 0;
        }
      }
    }
  }

  NO;
  return 0;
}


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

using namespace std;

#define YES cout << "YES" << endl;
#define NO cout << "NO" << endl;
#define Yes cout << "Yes" << endl;
#define No cout << "No" << endl;

#define INF INT_MAX
#define MOD 1000000007
#define PI acos(-1)

using ll = long long;
using ull = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  cin >> N;

  vector<ll> p(N);
  rep(i, N) { cin >> p[i]; }

  rep(k, N - 1) {
    if (p[k] > p[k + 1]) {
      break;
    }
    if (k == N - 2) {
      YES;
      return 0;
    }
  }

  rep(i, N) {
    for (ll j = i + 1; j < N; j++) {
      vector<ll> vec(N);
      copy(p.begin(), p.end(), vec.begin());
      ll tmp = vec[i];
      vec[i] = vec[j];
      vec[j] = tmp;
      rep(k, N - 1) {
        if (vec[k] > vec[k + 1]) {
          break;
        }
        if (k == N - 2) {
          YES;
          return 0;
        }
      }
    }
  }

  NO;
  return 0;
}
Problem p02958 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  int cnt = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> p[i];
    if (p[i] == i) {
      cnt += 1;
    }
  }
  if ((cnt == N) || (cnt == N - 2)) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> p(N + 1);
  int cnt = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> p[i];
    if (p[i] == i) {
      cnt += 1;
    }
  }
  if ((cnt == N) || (cnt == N - 2)) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }

  return 0;
}
Problem p02958 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int p, n, k;
int main() {
  scanf("%d", n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p);
    if (p != i) {
      k++;
    }
  }
  if (k > 2) {
    printf("NO");
  } else
    printf("YES");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int p, n, k;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &p);
    if (p != i) {
      k++;
    }
  }
  if (k > 2) {
    printf("NO");
  } else
    printf("YES");
  return 0;
}
Problem p02958 (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 < n; i++)
#define rep2(i, a, n) for (int i = (n); i < (a); i++)

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> hoge(n), list;
  rep(i, n) {
    cin >> hoge[i];
    list.push_back(i + 1);
  }
  // p$B$r>:=g$K$9$k$3$H$,$G$-$k$J$i(B
  // YES $B$r!"$G$-$J$$$J$i$P(B NO $B$r=PNO$7$F$/$@$5$$!#(B
  if (hoge == list) {
    cout << "YES" << endl;
    return 0;
  }
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == j)
        continue;
      vector<int> huga(hoge);
      swap(huga[i], huga[j]);
      if (huga == list) {
        cout << "YES" << endl;
        return 0;
      }
    }
  }
  cout << "NO" << endl;
}


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

using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, a, n) for (int i = (n); i < (a); i++)

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> hoge(n), huga, list;
  rep(i, n) cin >> hoge[i];
  int k = 0;
  rep(i, n) if (hoge[i] != i + 1) k++;
  cout << (k <= 2 ? "YES" : "NO") << endl;
}
Problem p02958 (C++)

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

int main() {
  long int N, cnt = 0, a, b, tmp;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    if (A[i] > A[i + 1]) {
      ++cnt;
      if (cnt == 1) {
        a = i;
      } else if (cnt == 2) {
        b = i + 1;
      }
    }
  }

  if (cnt > 2) {
    cout << "NO" << endl;
  } else if (cnt == 0)
    cout << "YES" << endl;
  else {
    tmp = A[a];
    A[a] = A[b];
    A[b] = A[tmp];
    for (int i = 0; i < N - 1; i++) {
      if (A[i] > A[i + 1]) {
        cout << "NO" << endl;
        break;
      }
      if (i == N - 2)
        cout << "YES" << endl;
    }
  }
}

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

int main() {
  long int N, cnt = 0, a, b, tmp;
  cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N - 1; i++) {
    if (A[i] > A[i + 1]) {
      ++cnt;
      if (cnt == 1) {
        a = i;
      } else if (cnt == 2) {
        b = i + 1;
      }
    }
  }

  if (cnt > 2) {
    cout << "NO" << endl;
  } else if (cnt == 0)
    cout << "YES" << endl;
  else if (cnt == 1) {
    tmp = A[a];
    A[a] = A[a + 1];
    A[a + 1] = A[tmp];
    for (int i = 0; i < N - 1; i++) {
      if (A[i] > A[i + 1]) {
        cout << "NO" << endl;
        break;
      }
      if (i == N - 2)
        cout << "YES" << endl;
    }
  }

  else {
    tmp = A[a];
    A[a] = A[b];
    A[b] = A[tmp];
    for (int i = 0; i < N - 1; i++) {
      if (A[i] > A[i + 1]) {
        cout << "NO" << endl;
        break;
      }
      if (i == N - 2)
        cout << "YES" << endl;
    }
  }
}
Problem p02958 (C++)

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

int main() {
  int N, check = 0;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < N; i++) {
    if (a.at(i) != i + 1) {
      swap(a.at(i), a.at(a.at(i)));
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    if (a.at(i) != i + 1) {
      check = 1;
      break;
    }
  }
  if (check == 1) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
  }
}

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

int main() {
  int N, check = 0;
  cin >> N;
  vector<int> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  for (int i = 0; i < N; i++) {
    if (a.at(i) != i + 1) {
      swap(a.at(i), a.at(a.at(i) - 1));
      break;
    }
  }
  for (int i = 0; i < N; i++) {
    if (a.at(i) != i + 1) {
      check = 1;
      break;
    }
  }
  if (check == 1) {
    cout << "NO" << endl;
  } else {
    cout << "YES" << endl;
  }
}
Problem p02958 (C++)

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

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

  vector<int> p;

  if (n == 2) {
    cout << "YES" << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> pi;
    p.push_back(pi);
  }

  int first = -1, second = -1, temp;

  for (int i = 1; i < n; i++) {
    if (p[i] < p[i - 1]) {
      first = i;
      break;
    }
  }

  if (first == -1) {
    cout << "YES" << endl;
    return 0;
  }

  for (int i = first + 1; i < n; i++) {
    if (p[i] < p[i - 1]) {
      second = i;
      break;
    }
  }

  first--;

  temp = p[first];
  p[first] = p[second];
  p[second] = temp;

  for (int i = 1; i < n; i++) {
    if (p[i] < p[i - 1]) {
      cout << "NO" << endl;
      return 0;
    }
  }

  cout << "YES" << endl;
}


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

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

  vector<int> p;

  if (n == 2) {
    cout << "YES" << endl;
    return 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> pi;
    p.push_back(pi);
  }

  int first = -1, second = -1, temp;

  for (int i = 1; i < n; i++) {
    if (p[i] < p[i - 1]) {
      first = i;
      break;
    }
  }

  if (first == -1) {
    cout << "YES" << endl;
    return 0;
  }

  for (int i = first + 1; i < n; i++) {
    if (p[i] < p[i - 1]) {
      second = i;
      break;
    }
  }

  first--;

  if (second >= 0 && second <= n) {
    temp = p[first];
    p[first] = p[second];
    p[second] = temp;
  } else {
    temp = p[first];
    p[first] = p[first + 1];
    p[first + 1] = temp;
  }

  for (int i = 1; i < n; i++) {
    if (p[i] < p[i - 1]) {
      cout << "NO" << endl;
      return 0;
    }
  }

  cout << "YES" << endl;
}
Problem p02958 (C++)

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

bool check() {
  bool f = 1;
  REP(i, n - 1) if (a.at(i) > a.at(i + 1)) f = 0;
  return f;
}

void sw(int i, int j) {
  int tmp = a.at(i);
  a.at(i) = a.at(j);
  a.at(j) = tmp;
}

int main() {
  bool flag = 0;
  cin >> n;
  REP(i, n) cin >> a.at(i);
  if (check())
    flag = 1;

  REP(i, n - 1) for (int j = i + 1; j < n; j++) {
    sw(i, j);
    if (check())
      flag = 1;
    sw(i, j);
  }

  cout << (flag ? "YES" : "NO");
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int(i) = 0; i < (n); i++)
int n;
vector<int> a(50, 0);

bool check() {
  bool f = 1;
  REP(i, n - 1) if (a.at(i) > a.at(i + 1)) f = 0;
  return f;
}

void sw(int i, int j) {
  int tmp = a.at(i);
  a.at(i) = a.at(j);
  a.at(j) = tmp;
}

int main() {
  bool flag = 0;
  cin >> n;
  REP(i, n) cin >> a.at(i);
  if (check())
    flag = 1;

  REP(i, n - 1) for (int j = i + 1; j < n; j++) {
    sw(i, j);
    if (check())
      flag = 1;
    sw(i, j);
  }

  cout << (flag ? "YES" : "NO");
}
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;

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

    ll min2 = min(a[i + 1], b[i]);
    a[i + 1] -= min2;
    b[i] -= min2;
    ans += min2;
  }
  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;

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

    ll min2 = min(a[i + 1], b[i]);
    a[i + 1] -= min2;
    b[i] -= min2;
    ans += min2;
  }
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
//============================
// ABC135c City Savers
//
// 2019/08/13
//============================

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

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

  long long cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += min(A[i], B[i]);
    B[i] -= min(A[i], B[i]);
    cnt += min(A[i + 1], B[i]);
    A[i + 1] -= min(A[i + 1], B[i]);
  }
  cout << cnt << endl;

  return 0;
}

Fixed submission:
//============================
// ABC135c City Savers
//
// 2019/08/13
//============================

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int N;
  cin >> N;

  //  long A[100000], B[100000];
  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];

  long long cnt = 0;
  for (int i = 0; i < N; i++) {
    cnt += min(A[i], B[i]);
    B[i] -= min(A[i], B[i]);
    cnt += min(A[i + 1], B[i]);
    A[i + 1] -= min(A[i + 1], B[i]);
  }
  cout << cnt << endl;

  return 0;
}
Problem p02959 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];
  ll ans(0);
  rep(i, n) {
    if (a[i] >= b[i]) {
      ans += b[i];
    } else {
      ans += a[i];
      b[i] -= a[i];
      if (a[i + 1] >= b[i]) {
        ans += b[i];
        a[i + 1] -= b[i];
      } else {
        ans += a[i + i];
        a[i + 1] = 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <iostream>
#define rep(k, i, n) for (int i = k; i < n; ++i)
using namespace std;
typedef long long ll;
int N;
long a[10001], b[10000];
ll sum = 0;
int main(void) {
  // Your code here!
  cin >> N;
  rep(0, i, N + 1) cin >> a[i];
  rep(0, i, N) {
    cin >> b[i];
    if (b[i] < a[i]) {
      a[i] -= b[i];
      sum += b[i];
      b[i] = 0;
    } else {
      sum += a[i];
      b[i] -= a[i];
      a[i] = 0;
      if (a[i + 1] > b[i]) {
        sum += b[i];
        a[i + 1] -= b[i];
        b[i] = 0;
      } else {
        sum += a[i + 1];
        b[i] -= a[i + 1];
        a[i + 1] = 0;
      }
    }
  }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define rep(k, i, n) for (int i = k; i < n; ++i)
using namespace std;
typedef long long ll;
int N;
long a[100001], b[100000];
ll sum = 0;
int main(void) {
  // Your code here!
  cin >> N;
  rep(0, i, N + 1) cin >> a[i];
  rep(0, i, N) {
    cin >> b[i];
    if (b[i] < a[i]) {
      a[i] -= b[i];
      sum += b[i];
      b[i] = 0;
    } else {
      sum += a[i];
      b[i] -= a[i];
      a[i] = 0;
      if (a[i + 1] > b[i]) {
        sum += b[i];
        a[i + 1] -= b[i];
        b[i] = 0;
      } else {
        sum += a[i + 1];
        b[i] -= a[i + 1];
        a[i + 1] = 0;
      }
    }
  }
  cout << sum << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int ar[1000];
int br[1000];
int main() {
  ll n, ans = 0, res = 0, i;
  cin >> n;
  for (i = 0; i <= n; i++)
    cin >> ar[i];
  for (i = 0; i < n; i++)
    cin >> br[i];
  for (i = 0; i < n; i++) {
    if (ar[i] >= br[i])
      ans = ans + br[i];
    else {
      ans = ans + ar[i];
      res = br[i] - ar[i];
      if (res <= ar[i + 1]) {
        ar[i + 1] = ar[i + 1] - res;
        ans = ans + res;
      } else {
        ans = ans + ar[i + 1];
        ar[i + 1] = 0;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int ar[1000008];
int br[1000008];
int main() {
  ll n, ans = 0, res = 0, i;
  cin >> n;
  for (i = 0; i <= n; i++)
    cin >> ar[i];
  for (i = 0; i < n; i++)
    cin >> br[i];
  for (i = 0; i < n; i++) {
    if (ar[i] >= br[i])
      ans = ans + br[i];
    else {
      ans = ans + ar[i];
      res = br[i] - ar[i];
      if (res <= ar[i + 1]) {
        ar[i + 1] = ar[i + 1] - res;
        ans = ans + res;
      } else {
        ans = ans + ar[i + 1];
        ar[i + 1] = 0;
      }
    }
  }
  cout << ans << endl;
}
Problem p02959 (C++)

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

using namespace std;

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

  long long total = 0;
  for (int i = 0; i < n; i++) {
    total += min(a[i], b[i]);
    if (0 < b[i] - a[i]) {
      total += min(a[i + 1], b[i] - a[i]);
      a[i + 1] = max(a[i + 1] - b[i] + a[i], (long long)0);
    }
  }
  cout << total << endl;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long 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];

  long long total = 0;
  for (int i = 0; i < n; i++) {
    total += min(a[i], b[i]);
    if (0 < b[i] - a[i]) {
      total += min(a[i + 1], b[i] - a[i]);
      a[i + 1] = max(a[i + 1] - b[i] + a[i], (long long)0);
    }
  }
  cout << total << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

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

  ll ans = 0;
  rep(i, 0, n + 1) {
    ll x = min(b[i], a[i]);
    ans += x, a[i] -= x, b[i] -= x;
    ll y = min(b[i], a[i + 1]);
    ans += y, a[i + 1] -= y;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;
#define rep(i, a, n) for (ll i = (a); i < (n); i++)
#define debug(x) cerr << #x << ": " << x << endl;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

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

  ll ans = 0;
  rep(i, 0, n) {
    ll x = min(b[i], a[i]);
    ans += x, a[i] -= x, b[i] -= x;
    ll y = min(b[i], a[i + 1]);
    ans += y, a[i + 1] -= y;
  }
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
//
//  main.cpp
//  AC
//
//  Created by makinofuya on 2019/07/08.
//  Copyright © 2019 makinofuya. All rights reserved.
//
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
#define mod 1000000007
#define rep(i, n) for (int i = 1; i <= n; i++)
#define out(s) cout << s << endl

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  vector<int> b(n);

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


Fixed submission:
//
//  main.cpp
//  AC
//
//  Created by makinofuya on 2019/07/08.
//  Copyright © 2019 makinofuya. All rights reserved.
//
#include <algorithm>
#include <iostream>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
#define mod 1000000007
#define rep(i, n) for (int i = 1; i <= n; i++)
#define out(s) cout << s << endl

int main() {
  int n;
  cin >> n;
  vector<int> a(n + 2);
  vector<int> b(n + 1);

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

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

int n = 0, a[100000], b[100000];
long long ans = 0;

int main() {

  std::cin >> n;

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

  for (int i = 0; i < n; i++) {
    std::cin >> b[i];
  }

  for (int i = 0; i < n; i++) {

    if (a[i] >= b[i]) {
      ans += b[i];
    } else if (a[i + 1] + a[i] > b[i]) {
      ans += b[i];
      a[i + 1] = a[i + 1] + a[i] - b[i];
    } else {
      ans += a[i + 1] + a[i];
      a[i + 1] = 0;
    }
  }
  std::cout << ans << std::endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

int n = 0, a[100001], b[100000];
long long ans = 0;

int main() {

  std::cin >> n;

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

  for (int i = 0; i < n; i++) {
    std::cin >> b[i];
  }

  for (int i = 0; i < n; i++) {

    if (a[i] >= b[i]) {
      ans += b[i];
    } else if (a[i + 1] + a[i] > b[i]) {
      ans += b[i];
      a[i + 1] = a[i + 1] + a[i] - b[i];
    } else {
      ans += a[i + 1] + a[i];
      a[i + 1] = 0;
    }
  }
  std::cout << ans << std::endl;
}
Problem p02959 (C++)

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


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

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

using namespace std;

#define ll long long
#define ull unsigned long long
#define ar array

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

  int n;
  cin >> n;
  vector<int> a(n + 1), b(n);
  for (int &x : a)
    cin >> x;
  for (int &x : b)
    cin >> x;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int val = min(a[i], b[i]);
    a[i] -= val;
    b[i] -= val;
    ans += 1ll * val;
    if (b[i]) {
      val = min(a[i + 1], b[i]);
      a[i + 1] -= val;
      b[i] -= val;
      ans += 1ll * val;
    }
  }
  cout << ans << '\n';
}

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

using namespace std;

#define ll long long
#define ull unsigned long long
#define ar array

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

  int n;
  cin >> n;
  vector<int> a(n + 1), b(n);
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int val = min(a[i], b[i]);
    a[i] -= val;
    b[i] -= val;
    ans += 1ll * val;
    if (b[i]) {
      val = min(a[i + 1], b[i]);
      a[i + 1] -= val;
      b[i] -= val;
      ans += 1ll * val;
    }
  }
  cout << ans << '\n';
}
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<ll> 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];
  }

  ll ans = 0;
  for (int i = 0; i < n + 1; i++) {

    if (i != 0) {

      if (b[i - 1] <= a[i]) {
        ans += b[i - 1];
        a[i] -= b[i - 1];
        b[i - 1] = 0;
      } else { // a[i] < b[i]
        ans += a[i];
        b[i - 1] -= a[i];
        a[i] = 0;
      }
    }

    if (b[i] <= a[i]) {
      ans += b[i];
      a[i] -= b[i];
      b[i] = 0;
    } else { // a[i] < b[i]
      ans += a[i];
      b[i] -= a[i];
      a[i] = 0;
    }
  }

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

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

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

  vector<ll> 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];
  }

  ll ans = 0;
  for (int i = 0; i < n + 1; i++) {

    if (i != 0) {

      if (b[i - 1] <= a[i]) {
        ans += b[i - 1];
        a[i] -= b[i - 1];
        b[i - 1] = 0;
      } else { // a[i] < b[i]
        ans += a[i];
        b[i - 1] -= a[i];
        a[i] = 0;
      }
    }

    if (i != n) {

      if (b[i] <= a[i]) {
        ans += b[i];
        a[i] -= b[i];
        b[i] = 0;
      } else { // a[i] < b[i]
        ans += a[i];
        b[i] -= a[i];
        a[i] = 0;
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int main() {
  long long a, b[2000], c[2000], d, e, f = 0, g = 0, i, cnt = 0;
  scanf("%lld", &a);
  for (i = 0; i < a + 1; i++) {
    scanf("%lld", &b[i]);
    g += b[i];
  }
  for (i = 0; i < a; i++) {
    scanf("%lld", &c[i]);
    f += c[i];
  }
  for (i = 0; i < a; i++) {
    if (c[i] <= b[i]) {
      cnt += c[i];
      c[i] = 0;
    } else {
      cnt += b[i];
      c[i] -= b[i];
    }
    if (c[i] <= b[i + 1]) {
      cnt += c[i];
      b[i + 1] -= c[i];
    } else {
      cnt += b[i + 1];
      b[i + 1] = 0;
    }
  }
  printf("%lld\n", cnt);
  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int main() {
  long long a, b[100001], c[100000], d, e, f = 0, g = 0, i, cnt = 0;
  scanf("%lld", &a);
  for (i = 0; i < a + 1; i++) {
    scanf("%lld", &b[i]);
    g += b[i];
  }
  for (i = 0; i < a; i++) {
    scanf("%lld", &c[i]);
    f += c[i];
  }
  for (i = 0; i < a; i++) {
    if (c[i] <= b[i]) {
      cnt += c[i];
      c[i] = 0;
    } else {
      cnt += b[i];
      c[i] -= b[i];
    }
    if (c[i] <= b[i + 1]) {
      cnt += c[i];
      b[i + 1] -= c[i];
    } else {
      cnt += b[i + 1];
      b[i + 1] = 0;
    }
  }
  printf("%lld\n", cnt);
  return 0;
}
Problem p02959 (C++)

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

int main() {
  int n, a[1000], b[1000], an = 0;
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++) {
    if (a[i] >= b[i])
      an += b[i];
    else {
      an += a[i];
      b[i] -= a[i];
      if (a[i + 1] >= b[i]) {
        an += b[i];
        a[i + 1] -= b[i];
      } else {
        an += a[i + 1];
        a[i + 1] = 0;
      }
    }
  }
  cout << an << endl;
  return 0;
}


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

int main() {
  long long n, a[100001], b[100001], an = 0;
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++) {
    if (a[i] >= b[i])
      an += b[i];
    else {
      an += a[i];
      b[i] -= a[i];
      if (a[i + 1] >= b[i]) {
        an += b[i];
        a[i + 1] -= b[i];
      } else {
        an += a[i + 1];
        a[i + 1] = 0;
      }
    }
  }
  cout << an << endl;
  return 0;
}
Problem p02959 (C++)

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

typedef long long ll;

#define int ll
#define all(v) v.begin(), v.end()
#define reps(__i, a, b) for (int __i = a; i < b; i++)
#define rep(__i, n) reps(__i, 0, n)

const ll INF = (1ll << 60);
const ll MOD = (ll)1e9 + 7;

signed main() {
  int n;
  int a[10010], b[10010];
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= b[i]) {
      cnt += a[i];
      cnt += min(a[i + 1], b[i] - a[i]);
      a[i + 1] -= min(a[i + 1], b[i] - a[i]);
    } else {
      cnt += b[i];
    }
  }
  cout << cnt << endl;
}

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

typedef long long ll;

#define int ll
#define all(v) v.begin(), v.end()
#define reps(__i, a, b) for (int __i = a; i < b; i++)
#define rep(__i, n) reps(__i, 0, n)

const ll INF = (1ll << 60);
const ll MOD = (ll)1e9 + 7;

signed main() {
  int n;
  int a[100010], b[100010];
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];

  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= b[i]) {
      cnt += a[i];
      cnt += min(a[i + 1], b[i] - a[i]);
      a[i + 1] -= min(a[i + 1], b[i] - a[i]);
    } else {
      cnt += b[i];
    }
  }
  cout << cnt << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n + 1), b(n);
  for (int i = 1; i <= n + 1; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    int left = min(a[i], b[i]);
    sum += left;
    a[i] -= left;
    b[i] -= left;

    int right = min(a[i + 1], b[i]);
    sum += right;
    a[i + 1] -= right;
    b[i] -= right;
  }

  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void) {
  int n;
  cin >> n;
  int a[n], b[n + 1];
  // vector<int> a(n+1), b(n);
  for (int i = 1; i <= n + 1; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    cin >> b[i];
  ll sum = 0;
  for (int i = 1; i <= n; i++) {
    int left = min(a[i], b[i]);
    sum += left;
    a[i] -= left;
    b[i] -= left;

    int right = min(a[i + 1], b[i]);
    sum += right;
    a[i + 1] -= right;
    b[i] -= right;
  }

  cout << sum << endl;
}
Problem p02959 (C++)

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

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

Buggy submission (status: Runtime Error):
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

using namespace std;

int main() {

  int a[51], sum, n, flag = 0, m, count = 0;

  cin >> n;

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

  for (int i = 0; i < n; i++) {
    cin >> m;
    if (a[i] >= m) {
      count = count + m;
      continue;
    } else {
      if (m >= a[i] + a[i + 1]) {
        count = count + a[i] + a[i + 1];
        a[i + 1] = 0;
      } else {
        a[i + 1] = a[i + 1] - (m - a[i]);
        count = count + m;
      }
    }
  }

  cout << count << endl;

  return 0;
}


Fixed submission:
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

using namespace std;

int main() {

  long a[100002], sum, n, flag = 0, m, count = 0;

  cin >> n;

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

  for (int i = 0; i < n; i++) {
    cin >> m;
    if (a[i] >= m) {
      count = count + m;
      continue;
    } else {
      if (m >= a[i] + a[i + 1]) {
        count = count + a[i] + a[i + 1];
        a[i + 1] = 0;
      } else {
        a[i + 1] = a[i + 1] - (m - a[i]);
        count = count + m;
      }
    }
  }

  cout << count << endl;

  return 0;
}
Problem p02959 (C++)

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl;

int main() {
  int N;
  cin >> N;
  vector<long long int> A(N), B(N);

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

  long long int count = 0;

  for (int i = 1; i <= N; i++) {
    bool flag = 0;
    int disc1 = A[i] - B[i];
    if (disc1 >= 0) {
      count += B[i];
      flag = 1;
    } else {
      count += A[i];
      B[i] -= A[i];
    }

    int disc2 = A[i + 1] - B[i];
    if (flag == 0) {
      if (disc2 >= 0) {
        count += B[i];
        A[i + 1] -= B[i];
      } else {
        count += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }

  OP(count)

  return 0;
}


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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl;

int main() {
  int N;
  cin >> N;
  int A[N], B[N];

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

  long long int count = 0;

  for (int i = 1; i <= N; i++) {
    bool flag = 0;
    int disc1 = A[i] - B[i];
    if (disc1 >= 0) {
      count += B[i];
      flag = 1;
    } else {
      count += A[i];
      B[i] -= A[i];
    }

    int disc2 = A[i + 1] - B[i];
    if (flag == 0) {
      if (disc2 >= 0) {
        count += B[i];
        A[i + 1] -= B[i];
      } else {
        count += A[i + 1];
        A[i + 1] = 0;
      }
    }
  }

  OP(count)

  return 0;
}
Problem p02959 (C++)

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

using namespace std;

int main(int argc, char const *argv[]) {
  long long int n, A[10000], B[10000], c = 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) {
    long long int h = min(A[i], B[i]);
    A[i] -= h;
    B[i] -= h;
    c += h;
    h = min(A[i + 1], B[i]);
    A[i + 1] -= h;
    c += h;
  }
  cout << c;
  return 0;
}

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

using namespace std;

int main(int argc, char const *argv[]) {
  long long int n, A[100007], B[100007], c = 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) {
    long long int h = min(A[i], B[i]);
    A[i] -= h;
    B[i] -= h;
    c += h;
    h = min(A[i + 1], B[i]);
    A[i + 1] -= h;
    c += h;
  }
  cout << c;
  return 0;
}
Problem p02959 (C++)

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

int main() {
  int n;
  cin >> n;
  int a[n + 1], b[n];
  rep(i, 0, n + 1) cin >> a[i];
  rep(i, 0, n) cin >> b[i];

  ll ans = 0;
  int f[n][n + 1];
  f[0][0] = min(a[0], b[0]);
  f[0][1] = min(a[1], b[0] - f[0][0]);
  ans += f[0][0];
  ans += f[0][1];
  rep(i, 1, n) {
    f[i][i] = min(a[i], b[i] - f[i - 1][i]);
    f[i][i + 1] = min(a[i + 1], b[i] - f[i][i]);
    ans += f[i][i];
    ans += f[i][i + 1];
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  int a[n + 1], b[n];
  rep(i, 0, n + 1) cin >> a[i];
  rep(i, 0, n) cin >> b[i];

  ll ans = 0;
  rep(i, 0, n) {
    int left = min(a[i], b[i]);
    ans += left;
    a[i] -= left;
    b[i] -= left;

    int right = min(a[i + 1], b[i]);
    ans += right;
    a[i + 1] -= right;
    b[i] -= right;
  }
  cout << ans << endl;
  return 0;
}
Problem p02959 (C++)

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

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

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

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


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

int main() {
  int N;
  cin >> N;
  vector<long long> A(N + 1);
  vector<long long> B(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;
  for (int i = 0; i < N; i++) {
    if (A[i] < B[i]) {
      ans += A[i];
      B[i] = B[i] - A[i];
      A[i] = 0;
      if (B[i] < A[i + 1]) {
        ans += B[i];
        A[i + 1] = A[i + 1] - B[i];
        B[i] = 0;

      } else if (B[i] >= A[i + 1]) {
        ans += A[i + 1];
        B[i] = B[i] - A[i + 1];
        A[i + 1] = 0;
      }
    }
    if (A[i] >= B[i]) {
      ans += B[i];
      A[i] = A[i] - B[i];
      B[i] = 0;
    }
  }
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, s[100000], t[100000];
long long ans;
int main(void) {
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> s[i];
  for (int i = 0; i < n; i++)
    cin >> t[i];
  for (int i = 0; i < n; i++) {
    ans += min(t[i], s[i]);
    t[i] = max(t[i] - s[i], 0);
    ans += min(t[i], s[i + 1]);
    s[i + 1] = max(s[i + 1] - t[i], 0);
  }
  cout << ans << endl;
}


Fixed submission:
#include <iostream>
using namespace std;
int n, s[100001], t[100000];
long long ans;
int main(void) {
  cin >> n;
  for (int i = 0; i <= n; i++)
    cin >> s[i];
  for (int i = 0; i < n; i++)
    cin >> t[i];
  for (int i = 0; i < n; i++) {
    ans += min(t[i], s[i]);
    t[i] = max(t[i] - s[i], 0);
    ans += min(t[i], s[i + 1]);
    s[i + 1] = max(s[i + 1] - t[i], 0);
  }
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = int(n); i < i##_len; ++i)
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N), B(N);
  REP(i, N + 1) cin >> A[i];
  REP(i, N) cin >> B[i];
  ll ans = 0;
  REP(i, N) {
    ans += min(A[i], B[i]);
    B[i] -= A[i];
    ans += min(A[i + 1], max(B[i], 0LL));
    A[i + 1] -= min(A[i + 1], max(B[i], 0LL));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = int(n); i < i##_len; ++i)
#define rep(i, a, b) for (int i = int(a); i < int(b); ++i)
#define All(x) (x).begin(), (x).end()
#define rAll(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;

int main() {
  int N;
  cin >> N;
  vector<ll> A(N + 1), B(N);
  REP(i, N + 1) cin >> A[i];
  REP(i, N) cin >> B[i];
  ll ans = 0;
  REP(i, N) {
    ans += min(A[i], B[i]);
    B[i] -= A[i];
    ans += min(A[i + 1], max(B[i], 0LL));
    A[i + 1] -= min(A[i + 1], max(B[i], 0LL));
  }
  cout << ans << endl;
}
Problem p02959 (C++)

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

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

int main() {

  std::cin >> n;

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

  for (int i = 0; i < n; i++) {
    std::cin >> b[i];
  }

  for (int i = 0; i < n; i++) {

    if (a[i] >= b[i]) {
      ans += b[i];
    } else if (a[i + 1] + a[i] > b[i]) {
      ans += b[i];
      a[i + 1] = a[i + 1] + a[i] - b[i];
    } else {
      ans += a[i + 1] + a[i];
      a[i + 1] = 0;
    }
  }
  std::cout << ans << std::endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>

int n = 0, a[100001], b[100000];
long long ans = 0;

int main() {

  std::cin >> n;

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

  for (int i = 0; i < n; i++) {
    std::cin >> b[i];
  }

  for (int i = 0; i < n; i++) {

    if (a[i] >= b[i]) {
      ans += b[i];
    } else if (a[i + 1] + a[i] > b[i]) {
      ans += b[i];
      a[i + 1] = a[i + 1] + a[i] - b[i];
    } else {
      ans += a[i + 1] + a[i];
      a[i + 1] = 0;
    }
  }
  std::cout << ans << std::endl;
}