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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define rSort(a) sort(a.rbegin(), a.rend())
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define Sum(a) accumulate(a.begin(), a.end(), 0)
#define REP(i, n) for (UI i = 0; i < n; i++)
#define REPR(i, n) for (UI i = n; i >= 0; i--)
#define FOR(i, m, n) for (UI i = m; i < n; i++)
#define pb(i) push_back(i)
#define SWAP(x, y) ((x != y) && (x ^= y, y ^= x, x ^= y))

#define MOD (LL)1e9 + 7;
#define INF (LL)1 << 62;

int main(int argc, char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  LL a, b, x;
  LL ans = 0;

  cin >> a >> b >> x;

  for (LL i = a; i <= b; i++) {
    if (i % x == 0) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


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

using namespace std;

typedef long long LL;
typedef unsigned int UI;

#define rSort(a) sort(a.rbegin(), a.rend())
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define Sum(a) accumulate(a.begin(), a.end(), 0)
#define REP(i, n) for (UI i = 0; i < n; i++)
#define REPR(i, n) for (UI i = n; i >= 0; i--)
#define FOR(i, m, n) for (UI i = m; i < n; i++)
#define pb(i) push_back(i)
#define SWAP(x, y) ((x != y) && (x ^= y, y ^= x, x ^= y))

#define MOD (LL)1e9 + 7;
#define INF (LL)1 << 62;

int main(int argc, char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  LL a, b, x;
  LL ans = 0;

  cin >> a >> b >> x;

  ans = floor((long double)b / x) - ceil((long double)a / x) + 1;

  cout << ans << endl;

  return 0;
}
Problem p03861 (C++)

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

int main(void) {
  long long a, b, x, sum = 0;
  cin >> a >> b >> x;

  for (int i = a; i <= b; i++) {
    if (i % i == 0)
      sum++;
  }
  cout << sum << endl;
  return 0;
}


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

int main(void) {
  long long a, b, x, sum = 0;
  cin >> a >> b >> x;

  if (a == 0) {
    sum += b / x + 1;
  } else
    sum += b / x - (a - 1) / x;
  cout << sum << endl;
  return 0;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdlib>
#include <iostream>

using namespace std;

int main(void) {
  long a, b, x;
  cin >> a >> b >> x;

  long res = 0;
  for (long i = a; i <= b; i++) {
    if (i % x == 0) {
      res = 1 + (b - i) / x;
      break;
    }
  }
  cout << res << endl;
  return EXIT_SUCCESS;
}


Fixed submission:
#include <cstdlib>
#include <iostream>

using namespace std;

int main(void) {
  long a, b, x;
  cin >> a >> b >> x;

  long res = 0;
  for (long i = a; i <= b; i += x - i % x) {
    if (i % x == 0) {
      res = 1 + (b - i) / x;
      break;
    }
  }
  cout << res << endl;
  return EXIT_SUCCESS;
}
Problem p03861 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll a, b, x;
  cin >> a >> b >> x;
  ll aa = x / a;
  ll bb = b / x;
  ll c = a % x;
  ll d = b % x;
  if (c == 0 && d == 0)
    cout << bb - aa + 1 << endl;
  if (c == 0 && d != 0)
    cout << bb - aa + 1 << endl;
  if (c != 0 && d == 0)
    cout << bb - aa << endl;
  if (c != 0 && d != 0)
    cout << bb - aa << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll a, b, x;
  cin >> a >> b >> x;
  ll aa = a / x;
  ll bb = b / x;
  ll c = a % x;
  ll d = b % x;
  if (c == 0 && d == 0)
    cout << bb - aa + 1 << endl;
  if (c == 0 && d != 0)
    cout << bb - aa + 1 << endl;
  if (c != 0 && d == 0)
    cout << bb - aa << endl;
  if (c != 0 && d != 0)
    cout << bb - aa << endl;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ll a, b, k, count = 0;
  cin >> a >> b >> k;
  for (ll i = a; i <= b; i++) {
    if (i % k == 0) {
      count++;
    }
  }
  cout << count << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
  ll a, b, x, ans = 0;
  cin >> a >> b >> x;
  if (a == 0)
    ans = 1 + (b / x);
  else
    ans = (b / x) - ((a - 1) / x);

  cout << ans << endl;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long a, b, x, ans = 0;
  cin >> a >> b >> x;
  for (int i = a; i <= b; i++)
    ans += !(i % x);
  cout << ans << endl;
}

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

using namespace std;

int main() {
  long long a, b, x, ans = 0;
  cin >> a >> b >> x;
  long long t1 = b / x;
  long long t2 = a / x - !(a % x);
  cout << t1 - t2 << endl;
}
Problem p03861 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < (int)c; i++)
#define all(v) v.begin(), v.end()
const int inf = 1000000000; // 10^9
using ll = long long;
using P = pair<int, int>;
// 加算代入演算子は+=！！！！！

int main() {
  ll a, b, x, c;
  cin >> a >> b >> c;
  c = b / x - a / x; // 答えはa以上b以下だけどこの式だとaが含まれない
  if (a % x != 0) // aがxで割り切れないときはそのままでいいけど
  {
    cout << c << endl;
  } else // aがxで割り切れる場合は、+1しなきゃいけない
  {
    cout << c + 1 << endl;
  }
  return 0;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < (int)c; i++)
#define all(v) v.begin(), v.end()
const int inf = 1000000000; // 10^9
using ll = long long;
using P = pair<int, int>;
// 加算代入演算子は+=！！！！！

int main() {
  ll a, b, x, c;
  cin >> a >> b >> x;
  c = b / x - a / x; // 答えはa以上b以下だけどこの式だとaが含まれない
  if (a % x != 0) // aがxで割り切れないときはそのままでいいけど
  {
    cout << c << endl;
  } else // aがxで割り切れる場合は、+1しなきゃいけない
  {
    cout << c + 1 << endl;
  }
  return 0;
}
Problem p03861 (C++)

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

int main() {
  long long a, b, x;
  cin >> a >> b >> x;
  long long ans = 0;
  for (long long i = a; i <= b; i++) {
    if (i % x == 0)
      ans++;
  }
  cout << ans << endl;
}

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

int main() {
  long long a, b, x;
  cin >> a >> b >> x;
  long long ans = b / x - a / x;
  if (a % x == 0)
    ans++;
  cout << ans << endl;
}
Problem p03861 (C++)

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPL(i, f, n) for (int i = f, i##_len = (n); i < i##_len; ++i)
typedef long long ll;

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

  int a, b, x;
  cin >> a >> b >> x;

  cout << b / x + 1 - (a > 0 ? (a - 1) / x + 1 : 0) << endl;
  return 0;
}


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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPL(i, f, n) for (int i = f, i##_len = (n); i < i##_len; ++i)
typedef long long ll;

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

  ll a, b, x;
  cin >> a >> b >> x;

  cout << b / x + 1 - (a > 0 ? (a - 1) / x + 1 : 0) << endl;
  return 0;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <string>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define shosu(n) setprecision(n)
using namespace std;

int main() {
  long long a, b, x, ans = 0;
  cin >> a >> b >> x;
  for (long long i = a; i <= b; i++) {
    if (i % x == 0) {
      ans += 1;
    }
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  long long a, b, x, ans = 0;
  cin >> a >> b >> x;
  if (a == 0)
    cout << b / x + 1 << endl;
  else
    cout << b / x - (a - 1) / x << endl;
  return 0;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#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>;
// pow(x,n)
// cout << fixed << setprecision(桁数);

int main() {
  ll a, b, x;
  cin >> a >> b >> x;
  ll min, max;
  for (ll i = a; i <= b; ++i) {
    if (i % x == 0) {
      min = i;
      break;
    }
    if (i == b && i % x != 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  for (ll i = b; i >= a; --i) {
    if (i % x == 0) {
      max = i;
      break;
    }
  }
  ll kai = (max - min) / x + 1;
  // cout << "min" << min << "max: " << max << endl;
  cout << kai << 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>;
// pow(x,n)
// cout << fixed << setprecision(桁数);

int main() {
  ll a, b, x;
  cin >> a >> b >> x;

  ll min;
  if (a == 0)
    min = -1;
  else
    min = (a - 1) / x;
  ll max = (b) / x;
  ll out = max - min;
  cout << out << endl;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll a, b, x;
  cin >> a >> b >> x;

  ll ans = 0;
  for (ll i = a; i <= b; i++) {
    if (i % x == 0)
      ans++;
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll a, b, x;
  cin >> a >> b >> x;
  ll ans;
  if (a % x == 0)
    ans = 1 + b / x - a / x;
  else
    ans = b / x - a / x;

  cout << ans << endl;
}
Problem p03861 (C++)

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

#define rep(i, n) for ((i) = 0; (i) < (n); ++(i))
#define repr(i, n) for ((i) = (n); (i) > 0; == (i))
#define FOR(i, j, n) for ((i) = (j); (i) < (n); ++(i))
#define forr(i, j, n) for ((i) = (j); (i) > (n); --(i))
#define debg(a) cout << (a) << endl;
#define INF 9999999
#define int long long
int i, j, k;
int dx, dy;
int a, b, c, x, y, n, m, cnt = 0, ans = 0;
string s, t;

signed main() {
  cin >> a >> b >> x;
  for (i = a; i <= b; ++i) {
    if (i % x == 0)
      cnt++;
  }
  debg(cnt);
  return 0;
}


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

#define rep(i, n) for ((i) = 0; (i) < (n); ++(i))
#define repr(i, n) for ((i) = (n); (i) > 0; == (i))
#define FOR(i, j, n) for ((i) = (j); (i) < (n); ++(i))
#define forr(i, j, n) for ((i) = (j); (i) > (n); --(i))
#define debg(a) cout << (a) << endl;
#define INF 9999999
#define int long long
int i, j, k;
int dx, dy;
int a, b, c, x, y, n, m, cnt = 0, ans = 0;
string s, t;

signed main() {
  cin >> a >> b >> x;
  cout << (a == 0 ? b / x + 1 : b / x - (a - 1) / x) << endl;

  return 0;
}
Problem p03861 (C++)

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

int main() {
  int64_t a, b, x;
  cin >> a >> b >> x;
  int64_t count = 0;
  for (int64_t i = a; i <= b; i++) {
    if (i % x == 0)
      count++;
  }
  cout << count << endl;
}


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

int main() {
  int64_t a, b, x;
  cin >> a >> b >> x;
  int i = 0;
  if (a % x == 0)
    i = 1;
  cout << i + b / x - a / x << endl;
}
Problem p03861 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string SA, SB, SC;
  cin >> SA >> SB >> SC;
  if (SA == "c" || SA == "b")
    cout << "A" << endl;
  else {
    queue<char> A, B, C;
    for (int i = 0; i < SA.size(); i++)
      A.push(SA[i]);
    for (int i = 0; i < SB.size(); i++)
      B.push(SB[i]);
    for (int i = 0; i < SC.size(); i++)
      C.push(SC[i]);
    int i = 0;
    char x = A.front();
    while (i == 0) {
      if (x == 'a') {
        A.pop();
        x = A.front();
      } else if (x == 'b') {
        B.pop();
        x = B.front();
      } else if (x == 'c') {
        C.pop();
        x = C.front();
      }
      if (A.size() == 0) {
        cout << 'A' << endl;
        return 0;
      } else if (B.size() == 0) {
        cout << 'B' << endl;
        return 0;
      } else if (C.size() == 0) {
        cout << 'C' << endl;
        return 0;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, x;
  cin >> a >> b >> x;
  if (a == 0)
    cout << b / x + 1 << endl;
  else
    cout << b / x - (a - 1) / x << endl;
}
Problem p03861 (C++)

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  if (a == 0) {
    cout << b / c + 1 << endl;
  } else {
    cout << b / c - (a - 1) / c << endl;
  }
}


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

int main() {
  long long a, b, c;
  cin >> a >> b >> c;
  if (a == 0) {
    cout << b / c + 1 << endl;
  } else {
    cout << b / c - (a - 1) / c << endl;
  }
}
Problem p03861 (C++)

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

int main() {
  long long a, b, x, ax, bx;
  cin >> a >> b >> x;
  while (true) {
    if (a % x == 0) {
      ax = a;
      break;
    }
    a++;
  }
  while (true) {
    if (b % x == 0) {
      bx = b;
      break;
    }
    b--;
  }
  if (a <= b) {
    cout << (b - a) / x + 1 << "\n";
  } else {
    cout << 0 << "\n";
  }
}

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

int main() {
  long long a, b, x, ax, bx;
  cin >> a >> b >> x;
  ax = (a + x - 1) / x;
  bx = b / x;
  if (ax <= bx) {
    cout << (bx - ax) + 1 << "\n";
  } else {
    cout << 0 << "\n";
  }
}
Problem p03861 (C++)

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

int main() {
  long long int a, b, x;
  cin >> a >> b >> x;
  cout << x / b - x / a + (x % a == 0 ? 1 : 0) << endl;
  return 0;
}

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

int main() {
  long long int a, b, x;
  cin >> a >> b >> x;
  cout << b / x - a / x + (a % x == 0 ? 1 : 0) << endl;
  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, x, a[100010];
  cin >> n >> x;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long cnt = 0;
  for (long long i = 1; i < n;) {
    if (a[i - 1] + a[i] > x) {
      a[i]--;
      // cout << "a[" << i+1 << "]は" << a[i+1] << endl;
      cnt++;
    } else if (a[i - 1] + a[i] <= x) {
      i++;
    }
  }
  cout << cnt << endl;
  return 0;
}

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
const long long MOD = 1e9 + 7;

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
const long long MOD = 1e9 + 7;

int main() {
  ll n, x;
  cin >> n >> x;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  ll ans = max(0LL, a[0] - x);
  a[0] = min(a[0], x);
  REP(i, n - 1) {
    if (a[i] + a[i + 1] > x) {
      ll diff = a[i + 1] + a[i] - x;
      a[i + 1] -= diff;
      ans += diff;
    }
  }
  cout << ans << endl;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// ll mod = 1e9+7;
ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

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


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
// ll mod = 1e9+7;
ll mod = 998244353;
#define rep(i, n) for (int i = 0; i < (n); ++i)

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

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

using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)

int main() {
  ll N, x;
  cin >> N >> x;
  ll a[10000];
  ll cnt = 0;
  rep(i, 0, N) {
    cin >> a[i];
    if (i == 0) {
      if (x < a[i]) {
        ll t = a[i] - x;
        a[i] -= t;
        cnt += t;
      }
    } else if (i > 0) {
      if (x < a[i - 1] + a[i]) {
        ll t = a[i - 1] + a[i] - x;
        a[i] -= t;
        cnt += t;
      }
    }
  }
  cout << cnt << endl;

  return 0;
}

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

using namespace std;
typedef long long ll;
#define rep(i, s, n) for (ll i = s; i < n; i++)
#define repe(i, s, n) for (ll i = s; i <= n; i++)

int main() {
  ll N, x;
  cin >> N >> x;
  ll a[100000];
  ll cnt = 0;
  rep(i, 0, N) {
    cin >> a[i];
    if (i == 0) {
      if (x < a[i]) {
        ll t = a[i] - x;
        a[i] -= t;
        cnt += t;
      }
    } else if (i > 0) {
      if (x < a[i - 1] + a[i]) {
        ll t = a[i - 1] + a[i] - x;
        a[i] -= t;
        cnt += t;
      }
    }
  }
  cout << cnt << endl;

  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int solve() {
  long long result = 0;
  int n, x;
  cin >> n >> x;
  int a = 0;
  cin >> a;
  if (a > x) {
    result += a - x;
    a = x;
  }
  for (int i = 1, b = 0; i < n; ++i, a = b) {
    cin >> b;
    if (b > x) {
      result += b - x;
      b = x;
    }
    if (a + b > x) {
      int diff = a + b - x;
      result += diff;
      b -= diff;
    }
  }

  cout << result << endl;
  return 0;
}
int main(int argc, char **argv) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ::std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  solve();
  return 0;
}


Fixed submission:

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;

int solve() {
  long long result = 0;
  int n, x;
  cin >> n >> x;
  int a = 0;
  cin >> a;
  if (a > x) {
    result += a - x;
    a = x;
  }
  for (int i = 1, b = 0; i < n; ++i, a = b) {
    cin >> b;
    if (b > x) {
      result += b - x;
      b = x;
    }
    if (a + b > x) {
      int diff = a + b - x;
      result += diff;
      b -= diff;
    }
  }

  cout << result << endl;
  return 0;
}
int main(int argc, char **argv) {

  ::std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  std::cout.tie(0);
  solve();
  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, p, q) for (int i = p; i < q; i++)
#define ll long long

using namespace std;

// ABC 48 C

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

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define rep(i, p, q) for (int i = p; i < q; i++)
#define ll long long

using namespace std;

// ABC 48 C

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main() {
  ll n, x, s;
  in2(n, x);
  s = 0;
  vector<ll> a(n);
  rep(i, n) in(a[i]);
  if (a[0] > x) {
    s = a[0] - x;
    a[0] = x;
  }
  rep(i, n - 1) {
    while (a[i] + a[i + 1] > x) {
      s++;
      a[i + 1]--;
    }
  }
  out(s);
  memi;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define memi cout << endl
#define kono(n) cout << fixed << setprecision(n)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define hina cout << ' '
#define in(n) cin >> n
#define in2(n, m) cin >> n >> m
#define in3(n, m, l) cin >> n >> m >> l
#define out(n) cout << n
const ll mei = (ll)1e9 + 7;

int main() {
  ll n, x, s;
  in2(n, x);
  s = 0;
  vector<ll> a(n);
  rep(i, n) in(a[i]);
  if (a[0] > x) {
    s = a[0] - x;
    a[0] = x;
  }
  rep(i, n - 1) {
    if (a[i] + a[i + 1] > x) {
      s = s + (a[i] + a[i + 1] - x);
      a[i + 1] = x - a[i];
    }
  }
  out(s);
  memi;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll MOD = 1000000007;
ll INF = 1LL << 60;

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

int main() {
  ll N, x;
  cin >> N >> x;
  ll a[N];
  for (int i = 0; i < N; i++)
    cin >> a[i];
  ll count = 0;
  for (int i = 0; i < N - 1; i++) {
    if (a[i] + a[i + 1] > x) {

      while (a[i] + a[i + 1] > x) {
        if (a[i + 1] != 0)
          a[i + 1]--, count++;
        else
          a[i]--, count++;
      }
    }
  }
  cout << count << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll MOD = 1000000007;
ll INF = 1LL << 60;

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }

int main() {
  ll N, x;
  cin >> N >> x;
  ll a[N];
  for (int i = 0; i < N; i++)
    cin >> a[i];
  ll count = 0;
  for (int i = 0; i < N - 1; i++) {
    ll sum = a[i] + a[i + 1];
    if (sum > x) {
      ll diff = sum - x;
      if (diff <= a[i + 1])
        count += diff, a[i + 1] -= diff;
      else {
        a[i + 1] = 0;
        count += diff;
      }
    }
  }
  cout << count << endl;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) long long((x).size())
#define OUT(x) cout << (x) << endl
typedef long long ll;
typedef vector<ll> V;
typedef vector<vector<ll>> VV;
typedef pair<ll, ll> P;
typedef map<string, ll> M;
typedef unordered_map<ll, ll> HM;
typedef set<ll> S;
typedef multiset<ll> MS;
typedef queue<ll> Q;

int main() {
  ll n, x;
  cin >> n >> x;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 0;
  if (a[0] > x) {
    ans += a[0] - x;
    a[0] = x;
  }
  if (a[n - 1] > x) {
    ans += a[n - 1] - x;
    a[n - 1] = x;
  }
  for (ll i = 0; i < n - 1; i++) {
    while (a[i] + a[i + 1] > x) {
      a[i + 1]--;
      ans++;
    }
  }
  OUT(ans);
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) long long((x).size())
#define OUT(x) cout << (x) << endl
typedef long long ll;
typedef vector<ll> V;
typedef vector<vector<ll>> VV;
typedef pair<ll, ll> P;
typedef map<string, ll> M;
typedef unordered_map<ll, ll> HM;
typedef set<ll> S;
typedef multiset<ll> MS;
typedef queue<ll> Q;

int main() {
  ll n, x;
  cin >> n >> x;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  ll ans = 0;
  if (a[0] > x) {
    ans += a[0] - x;
    a[0] = x;
  }
  if (a[n - 1] > x) {
    ans += a[n - 1] - x;
    a[n - 1] = x;
  }
  for (ll i = 0; i < n - 1; i++) {
    if (a[i] + a[i + 1] > x) {
      ans += a[i] + a[i + 1] - x;
      a[i + 1] = x - a[i];
    }
  }
  OUT(ans);
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i, n) for (lli i = 0; i < n; i++)
#define loop(i, j, n) for (lli i = j; i < n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
// Function
lli combi(lli n, lli r) {
  if (r == 0 || r == n)
    return 1;
  else
    return combi(n - 1, r) + combi(n - 1, r - 1);
}
lli sigma1(lli n) { return n * (n + 1) / 2; }
lli sigma2(lli s, lli n) { return sigma1(n) - sigma1(s - 1); }
using namespace std;
// }}} End Header

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lli n, x, ans = 0;
  cin >> n >> x;
  vector<lli> a(n);
  rep(i, n) cin >> a[i];
  loop(i, 1, n) {
    while (true) {
      if (a[i - 1] + a[i] > x) {
        ans++;
        if (a[i] == 0)
          a[i - 1]--;
        else
          a[i]--;
      } else
        break;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i, n) for (lli i = 0; i < n; i++)
#define loop(i, j, n) for (lli i = j; i < n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
// Function
lli combi(lli n, lli r) {
  if (r == 0 || r == n)
    return 1;
  else
    return combi(n - 1, r) + combi(n - 1, r - 1);
}
lli sigma1(lli n) { return n * (n + 1) / 2; }
lli sigma2(lli s, lli n) { return sigma1(n) - sigma1(s - 1); }
using namespace std;
// }}} End Header

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lli n, x, ans = 0;
  cin >> n >> x;
  vector<lli> a(n);
  rep(i, n) cin >> a[i];
  loop(i, 1, n) {
    lli sum = a[i] + a[i - 1];
    lli dist = sum - x;
    if (dist > 0) {
      ans += dist;
      if (a[i] >= dist) {
        a[i] -= dist;
      } else {
        a[i - 1] -= (dist - a[i]);
        a[i] = 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

  int n, x;
  cin >> n >> x;

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

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    while (x < a[i] + a[i - 1]) {
      a[i]--;
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

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

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

  int n, x;
  cin >> n >> x;

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

  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    if (x < a[i] + a[i - 1]) {
      int diff = (a[i] + a[i - 1]) - x;
      a[i] -= diff;
      ans += diff;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

int main() {
  long long N;
  long long X;
  cin >> N >> X;
  long long A[10100];

  for (long i = 0; i < N; i++)
    cin >> A[i];
  long long res = 0;
  for (long i = 0; i < N - 1; i++) {
    if (A[i] + A[i + 1] > X) {
      long long tmp = abs(A[i] + A[i + 1] - X);
      res = res + tmp;
      if (tmp > A[i + 1]) {
        A[i] = A[i] - abs(tmp - A[i + 1]);
        A[i + 1] = 0;
      } else {
        A[i + 1] = A[i + 1] - tmp;
      }
    }
  }
  cout << res << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 1LL << 60;

long long gcd(long long a, long long b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

long long lcm(long long a, long long b) { return a * b / gcd(a, b); }

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

  for (long i = 0; i < N; i++)
    cin >> A[i];
  long long res = 0;
  for (long i = 0; i < N - 1; i++) {
    if (A[i] + A[i + 1] > X) {
      long long tmp = abs(A[i] + A[i + 1] - X);
      res = res + tmp;
      if (tmp > A[i + 1]) {
        A[i] = A[i] - abs(tmp - A[i + 1]);
        A[i + 1] = 0;
      } else {
        A[i + 1] = A[i + 1] - tmp;
      }
    }
  }
  cout << res << endl;
}
Problem p03862 (Python)

Buggy submission (status: Time Limit Exceeded):
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    while a[i] + a[i + 1] > x:
        if a[i + 1] != 0:
            ans += 1
            a[i + 1] -= 1
        else:
            ans += 1
            a[i] -= 1
print(ans)


Fixed submission:
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    if a[i] + a[i + 1] > x:
        ans += a[i + 1] + a[i] - x
        a[i + 1] = x - a[i]
        if a[i + 1] < 0:
            a[i + 1] = 0
print(ans)
Problem p03862 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (int i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)

/////////////////////////////////////////////////////////

void Main() {
  ll N, x;
  cin >> N >> x;
  vector<ll> a(N);
  rep(i, 0, N) cin >> a[i];
  vector<ll> s(N - 1);
  s[0] = a[0];
  rep(i, 1, N) s[i] = a[i - 1] + a[i];
  ll res = 0;
  rep(i, 0, N) {
    if (s[i] > x) {
      ll tmp = s[i] - x;
      res += tmp;
      s[i] -= tmp;
      if (i < N - 1)
        s[i + 1] -= tmp;
    }
  }
  cout << res << endl;
}

/////////////////////////////////////////////////////////

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << std::fixed << std::setprecision(15);

  Main();
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (int i = j; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1000000000)
#define MOD (1000000007)

/////////////////////////////////////////////////////////

void Main() {
  ll N, x;
  cin >> N >> x;
  vector<ll> a(N);
  rep(i, 0, N) cin >> a[i];
  vector<ll> s(N);
  s[0] = a[0];
  rep(i, 1, N) s[i] = a[i - 1] + a[i];
  ll res = 0;
  rep(i, 0, N) {
    if (s[i] > x) {
      ll tmp = s[i] - x;
      res += tmp;
      s[i] -= tmp;
      if (i < N - 1)
        s[i + 1] -= tmp;
    }
  }
  cout << res << endl;
}

/////////////////////////////////////////////////////////

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  // cout << std::fixed << std::setprecision(15);

  Main();
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_C main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX / 2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psi = pair<string, int>;
// functions

#endif

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

  int N, X;
  cin >> N >> X;
  vi A(N);
  REP(i, N) cin >> A[i];

  ll ans = 0;
  FOR(i, 1, N) {
    while (A[i - 1] + A[i] > X) {
      if (A[i] > 0)
        A[i]--;
      else
        A[i - 1]--;
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_C main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX / 2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psi = pair<string, int>;
// functions

#endif

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

  int N, X;
  cin >> N >> X;
  vi A(N);
  REP(i, N) cin >> A[i];

  ll ans = 0;
  FOR(i, 1, N) {
    if (A[i - 1] + A[i] > X) {
      int dif = A[i - 1] + A[i] - X;

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

  cout << ans << endl;

  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>

using namespace std;

typedef long long ll;

int main() {
  ll n, x;
  cin >> n >> x;
  ll *a = new ll[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll *b = new ll[n];
  for (int i = 0; i < n; i++)
    b[i] = a[i];

  if (b[0] > x)
    b[0] = x;

  for (int i = 0; i < n - 1; i++) {
    while (b[i] + b[i + 1] > x && b[i + 1] > 0)
      b[i + 1]--;
    while (i[b] + b[i + 1] > x && b[i] > 0)
      b[i]--;
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i] - b[i];
  }

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

Fixed submission:
#include <iostream>

using namespace std;

typedef long long ll;

int main() {
  ll n, x;
  cin >> n >> x;
  ll *a = new ll[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];

  ll *b = new ll[n];
  for (int i = 0; i < n; i++)
    b[i] = a[i];

  if (b[0] > x)
    b[0] = x;

  for (int i = 0; i < n - 1; i++) {
    if (b[i] + b[i + 1] > x)
      b[i + 1] -= min((b[i] + b[i + 1]) - x, b[i + 1]);
    if (b[i] + b[i + 1] > x && b[i] > 0)
      b[i] -= min(b[i] + b[i + 1] - x, b[i]);
  }

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ans += a[i] - b[i];
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;

#define INF 0x3f3f3f3f

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define DEBUG(vec)                                                             \
  for (auto v : vec) {                                                         \
    cout << v << " ";                                                          \
  }                                                                            \
  cout << endl;

using namespace std;

int a[10010] = {};

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

  int N, x;
  cin >> N >> x;
  REP(i, N) { cin >> a[i]; }
  ll ans = 0;
  if (a[0] > x) {
    ans += a[0] - x;
    a[0] = x;
  }
  REP(i, N - 1) {
    if (a[i] + a[i + 1] > x) {
      int diff = a[i] + a[i + 1] - x;
      ans += diff;
      a[i + 1] -= diff;
    }
  }

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


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;

#define INF 0x3f3f3f3f

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define DEBUG(vec)                                                             \
  for (auto v : vec) {                                                         \
    cout << v << " ";                                                          \
  }                                                                            \
  cout << endl;

using namespace std;

int a[100010] = {};

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

  int N, x;
  cin >> N >> x;
  REP(i, N) { cin >> a[i]; }
  ll ans = 0;
  if (a[0] > x) {
    ans += a[0] - x;
    a[0] = x;
  }
  REP(i, N - 1) {
    if (a[i] + a[i + 1] > x) {
      int diff = a[i] + a[i + 1] - x;
      ans += diff;
      a[i + 1] -= diff;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int n;
ll x, a[100000];
void solve() {
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    while (a[i] + a[i + 1] > x) {
      ans++;
      a[i + 1]--;
    }
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  solve();
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
int n;
ll x, a[100000];
void solve() {
  ll ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (a[i] + a[i + 1] > x) {
      ans += a[i] + a[i + 1] - x;
      a[i + 1] -= a[i] + a[i + 1] - x;
      if (a[i + 1] < 0) {
        a[i] += -a[i + 1];
        a[i + 1] = 0;
      }
    }
  }
  cout << ans << "\n";
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  solve();
}
Problem p03862 (C++)

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

int main() {
  // freopen("input16.txt","r",stdin);freopen("output16.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, x, i, a[10000], x1, s = 0;
  cin >> n >> x;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  for (i = 1; i < n; i++) {
    if (a[i] + a[i - 1] > x) {
      x1 = a[i] + a[i - 1] - x;
      if (x1 > a[i]) {
        x1 -= a[i];
        a[i] = 0;
        a[i - 1] -= x1;
      } else {
        a[i] -= x1;
      }
    }
  }
  for (i = 0; i < n; i++) {
    s -= a[i];
  }
  cout << s;
  return 0;
}

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

int main() {
  // freopen("input16.txt","r",stdin);freopen("output16.txt","w",stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ll n, x, i, a[100000], x1, s = 0;
  cin >> n >> x;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    s += a[i];
  }
  for (i = 1; i < n; i++) {
    if (a[i] + a[i - 1] > x) {
      x1 = a[i] + a[i - 1] - x;
      if (x1 > a[i]) {
        x1 -= a[i];
        a[i] = 0;
        a[i - 1] -= x1;
      } else {
        a[i] -= x1;
      }
    }
  }
  for (i = 0; i < n; i++) {
    s -= a[i];
  }
  cout << s;
  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, a, b) for (int i = a; i < (b); i++)
#define FOR(i, n) for (int i = n; i >= 0; i--)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define SIZE 100001
#define MOD 1000000007
#define INF 100000000
using namespace std;

int main() {
  int n, x;
  scanf("%d%d", &n, &x);

  vector<int> a(100001);

  REP(i, n) scanf("%d", &a[i]);

  ll cnt = 0;
  RREP(i, 1, n) {
    while (a[i - 1] + a[i] > x) {
      --a[i];
      cnt++;
    }
  }

  printf("%lld\n", cnt);

  return 0;
}

Fixed submission:
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, a, b) for (int i = a; i < (b); i++)
#define FOR(i, n) for (int i = n; i >= 0; i--)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define ll long long
#define SIZE 100001
#define MOD 1000000007
#define INF 100000000
using namespace std;

int main() {
  int n, x;
  scanf("%d%d", &n, &x);

  vector<int> a(100001);

  REP(i, n) scanf("%d", &a[i]);

  ll cnt = 0;
  RREP(i, 1, n) {
    if (a[i] + a[i - 1] > x) {
      int other = (a[i] + a[i - 1]) - x;
      if (other < 0)
        continue;
      else if (a[i] >= other) {
        a[i] -= other;
      } else if (a[i] < other) {
        a[i - 1] -= abs(other - a[i]);
        a[i] = 0;
      }
      cnt += other;
    }
  }

  printf("%lld\n", cnt);

  return 0;
}
Problem p03862 (C++)

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

int main() {
  long N, x, ans = 0;
  cin >> N >> x;
  vector<long> 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] > x) {
      while (a[i] + a[i + 1] > x && a[i + 1] > 0) {
        ans++;
        a[i + 1]--;
      }
      while (a[i] + a[i + 1] > x) {
        ans++;
        a[i]--;
      }
    }
  }
  cout << ans << endl;
}

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

int main() {
  long N, x, ans = 0;
  cin >> N >> x;
  vector<long> 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] > x) {
      ans += a[i] + a[i + 1] - x;
      a[i + 1] -= min(a[i + 1], a[i] + a[i + 1] - x);
    }
  }
  cout << ans << endl;
}