Problem p03862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = n; i >= 1; --i)
#define range(i, l, r) for (int i = l; i < (int)r; ++i)
#define rrange(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using ld = long double;

bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
constexpr int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int mod = TEN(9) + 7;

string convert(int a) {
  string ret = "";

  while (a) {
    ret.push_back(a % 10);
    a /= 10;
  }

  reverse(all(ret));

  return ret;
}

int S(int a) {
  int ret = 0;

  while (a)
    ret += a % 10, a /= 10;

  return ret;
}

double f(int a) { return (double)a / S(a); }

void output(int a) {
  printf("%lld: S(%lld) = %lld, f(%lld) = %f\n", a, a, S(a), a, f(a));
}

signed main() {
  int N, x;
  cin >> N >> x;
  vi a(N);
  rep(i, N) cin >> a[i];
  int ans = 0;
  ans += max(a[0] - x, 0ll);
  chmin(a[0], x);

  range(i, 1, N) {
    if (a[i] + a[i - 1] > x) {
      // 0 <= x - a[i - 1]
      ans += max(a[i] - (x - a[i - 1]), 0ll);
      chmin(a[i], a[i - 1] - x);
      if (a[i] + a[i - 1] > x)
        return 1;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = n; i >= 1; --i)
#define range(i, l, r) for (int i = l; i < (int)r; ++i)
#define rrange(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using ld = long double;

bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
constexpr int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

const int mod = TEN(9) + 7;

string convert(int a) {
  string ret = "";

  while (a) {
    ret.push_back(a % 10);
    a /= 10;
  }

  reverse(all(ret));

  return ret;
}

int S(int a) {
  int ret = 0;

  while (a)
    ret += a % 10, a /= 10;

  return ret;
}

double f(int a) { return (double)a / S(a); }

void output(int a) {
  printf("%lld: S(%lld) = %lld, f(%lld) = %f\n", a, a, S(a), a, f(a));
}

signed main() {
  int N, x;
  cin >> N >> x;
  vi a(N);
  rep(i, N) cin >> a[i];
  int ans = 0;
  ans += max(a[0] - x, 0ll);
  chmin(a[0], x);

  range(i, 1, N) {
    if (a[i] + a[i - 1] > x) {
      // 0 <= x - a[i - 1]
      int nxt_ai = x - a[i - 1];
      ans += max(a[i] - nxt_ai, 0ll);
      a[i] = nxt_ai;
      if (a[i] + a[i - 1] > x)
        return 1;
    }
  }

  cout << ans << endl;
}