Problem p02959 (C++)

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

#include <iomanip>
void _130() {
  int w, h, x, y;
  cin >> w >> h >> x >> y;

  cout << fixed << setprecision(10);
  cout << (double)w * h / 2 << " " << (2 * x == w && 2 * y == h) << endl;
}

void _131() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long count = 0;
  for (long long i = a; i <= b; ++i) {
    if (i % c != 0 && i % d != 0) {
      count++;
    }
  }
  cout << count << endl;
}

#include <cmath>
void _131_() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long min_baisuu;
  for (long long i = min(c, d); i <= c * d; ++i) {
    if (i % c == 0 && i % d == 0) {
      min_baisuu = i;
      cout << min_baisuu << endl;
      break;
    }
  }

  long long c_baisuu_num = 0;
  long long d_baisuu_num = 0;
  long long cd_baisuu_num = 0;

  c_baisuu_num = abs(a / c - b / c);
  d_baisuu_num = abs(a / d - b / d);
  cd_baisuu_num = abs(a / min_baisuu - b / min_baisuu);

  cout << abs(a - b) + 1 - (c_baisuu_num + d_baisuu_num - cd_baisuu_num)
       << endl;
}

void _131__() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long min_baisuu = 0;
  long long max_yakusuu = 0;

  long long amari = 0;
  long long bigger = max(c, d);
  long long smaller = min(c, d);
  while (true) {
    amari = bigger % smaller;
    if (amari == 0) {
      max_yakusuu = smaller;
      break;
    }
    bigger = smaller;
    smaller = amari;
  }
  // cout << max_yakusuu << endl;
  min_baisuu = c * d / max_yakusuu;

  long long b_num = b - b / c - b / d + b / min_baisuu;
  long long a_num = a - 1 - (a - 1) / c - (a - 1) / d + (a - 1) / min_baisuu;
  // cout << b_num << " " << a_num << endl;
  cout << b_num - a_num << endl;
}

#include <algorithm>
#include <vector>
void _132() {
  int n;
  cin >> n;
  vector<int> d;
  int tmp;
  for (int i = 0; i < n; ++i) {
    cin >> tmp;
    d.push_back(tmp);
  }
  sort(d.begin(), d.end());

  if (d.size() % 2 == 1) {
    cout << 0 << endl;
    return;
  }
  int half_index = d.size() / 2 - 1;
  cout << d[half_index + 1] - d[half_index] << endl;
}

void _133() {
  int l, r;
  cin >> l >> r;

  if (r - l > 2019) {
    cout << 0 << endl;
    return;
  }
  int min_mod = 2019;
  long long multi;
  long long mod;
  for (int i = l; i <= r; ++i) {
    for (int j = i + 1; j <= r; ++j) {
      multi = (long long)i * j;
      // multi = i * j;
      cout << multi << endl;
      mod = multi % 2019;
      // cout << i << " " << j << " " << mod <<endl;
      if (mod < min_mod) {
        min_mod = mod;
        // cout << min_mod << endl;
      }
    }
  }
  cout << min_mod << endl;
}

#include <algorithm>
#include <vector>
void _134() {
  int n;
  cin >> n;
  int tmp;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    cin >> tmp;
    a.push_back(tmp);
  }
  vector<int> b(a);
  sort(b.begin(), b.end());
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[n - 1])
      cout << b[n - 2] << endl;
    else
      cout << b[n - 1] << endl;
  }
}

void _135() {
  int n;
  int a[10001];
  int b[10000];
  cin >> n;
  for (int i = 0; i < n + 1; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];

  int residual_capa = 0;
  long long count = 0;
  int nokori = 0;
  for (int i = 0; i < n; ++i) {
    nokori = max(0, a[i] - residual_capa);
    if (nokori == 0) {
      count += a[i];
      residual_capa = b[i];
    } else {
      nokori = max(0, nokori - b[i]);
      // cout << nokori << endl;
      if (nokori == 0) {
        count += a[i];
        residual_capa = b[i] - (a[i] - residual_capa);
      } else {
        count += residual_capa + b[i];
        residual_capa = 0;
      }
    }
    // cout << residual_capa << " " << count <<  endl;
  }

  count += min(residual_capa, a[n]);
  cout << count << endl;
}

int main() {
  _135();
  return 0;
}


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

#include <iomanip>
void _130() {
  int w, h, x, y;
  cin >> w >> h >> x >> y;

  cout << fixed << setprecision(10);
  cout << (double)w * h / 2 << " " << (2 * x == w && 2 * y == h) << endl;
}

void _131() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long count = 0;
  for (long long i = a; i <= b; ++i) {
    if (i % c != 0 && i % d != 0) {
      count++;
    }
  }
  cout << count << endl;
}

#include <cmath>
void _131_() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long min_baisuu;
  for (long long i = min(c, d); i <= c * d; ++i) {
    if (i % c == 0 && i % d == 0) {
      min_baisuu = i;
      cout << min_baisuu << endl;
      break;
    }
  }

  long long c_baisuu_num = 0;
  long long d_baisuu_num = 0;
  long long cd_baisuu_num = 0;

  c_baisuu_num = abs(a / c - b / c);
  d_baisuu_num = abs(a / d - b / d);
  cd_baisuu_num = abs(a / min_baisuu - b / min_baisuu);

  cout << abs(a - b) + 1 - (c_baisuu_num + d_baisuu_num - cd_baisuu_num)
       << endl;
}

void _131__() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;

  long long min_baisuu = 0;
  long long max_yakusuu = 0;

  long long amari = 0;
  long long bigger = max(c, d);
  long long smaller = min(c, d);
  while (true) {
    amari = bigger % smaller;
    if (amari == 0) {
      max_yakusuu = smaller;
      break;
    }
    bigger = smaller;
    smaller = amari;
  }
  // cout << max_yakusuu << endl;
  min_baisuu = c * d / max_yakusuu;

  long long b_num = b - b / c - b / d + b / min_baisuu;
  long long a_num = a - 1 - (a - 1) / c - (a - 1) / d + (a - 1) / min_baisuu;
  // cout << b_num << " " << a_num << endl;
  cout << b_num - a_num << endl;
}

#include <algorithm>
#include <vector>
void _132() {
  int n;
  cin >> n;
  vector<int> d;
  int tmp;
  for (int i = 0; i < n; ++i) {
    cin >> tmp;
    d.push_back(tmp);
  }
  sort(d.begin(), d.end());

  if (d.size() % 2 == 1) {
    cout << 0 << endl;
    return;
  }
  int half_index = d.size() / 2 - 1;
  cout << d[half_index + 1] - d[half_index] << endl;
}

void _133() {
  int l, r;
  cin >> l >> r;

  if (r - l > 2019) {
    cout << 0 << endl;
    return;
  }
  int min_mod = 2019;
  long long multi;
  long long mod;
  for (int i = l; i <= r; ++i) {
    for (int j = i + 1; j <= r; ++j) {
      multi = (long long)i * j;
      // multi = i * j;
      cout << multi << endl;
      mod = multi % 2019;
      // cout << i << " " << j << " " << mod <<endl;
      if (mod < min_mod) {
        min_mod = mod;
        // cout << min_mod << endl;
      }
    }
  }
  cout << min_mod << endl;
}

#include <algorithm>
#include <vector>
void _134() {
  int n;
  cin >> n;
  int tmp;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    cin >> tmp;
    a.push_back(tmp);
  }
  vector<int> b(a);
  sort(b.begin(), b.end());
  for (int i = 0; i < n; ++i) {
    if (a[i] == b[n - 1])
      cout << b[n - 2] << endl;
    else
      cout << b[n - 1] << endl;
  }
}

void _135() {
  int n;
  int a[100001];
  int b[100000];
  cin >> n;
  for (int i = 0; i < n + 1; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];

  int residual_capa = 0;
  long long count = 0;
  int nokori = 0;
  for (int i = 0; i < n; ++i) {
    nokori = max(0, a[i] - residual_capa);
    if (nokori == 0) {
      count += a[i];
      residual_capa = b[i];
    } else {
      nokori = max(0, nokori - b[i]);
      // cout << nokori << endl;
      if (nokori == 0) {
        count += a[i];
        residual_capa = b[i] - (a[i] - residual_capa);
      } else {
        count += residual_capa + b[i];
        residual_capa = 0;
      }
    }
    // cout << residual_capa << " " << count <<  endl;
  }

  count += min(residual_capa, a[n]);
  cout << count << endl;
}

int main() {
  _135();
  return 0;
}