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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int64_t x;
  cin >> x;
  vector<int64_t> A(N);
  A.at(0) = 0;
  for (int i = 1; i <= N; i++)
    cin >> A.at(i);
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    if (x < A.at(i) + A.at(i + 1)) {
      int64_t y = A.at(i) + A.at(i + 1) - x;
      A.at(i + 1) -= y;
      ans += y;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int64_t x;
  cin >> x;
  vector<int64_t> A(N + 1);
  A.at(0) = 0;
  for (int i = 1; i <= N; i++)
    cin >> A.at(i);
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    if (x < A.at(i) + A.at(i + 1)) {
      int64_t y = A.at(i) + A.at(i + 1) - x;
      A.at(i + 1) -= y;
      ans += y;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
static int fast_io = []() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return 0;
}();
int main() {
  long long int n, x;
  cin >> n >> x;
  vector<long long int> candies(n);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> candies[i];
  }
  for (int i = 1; i < n - 1; i++) {
    while (candies[i - 1] + candies[i] > x) {
      if (candies[i] + candies[i + 1] > x) {
        long long int tmp = min(candies[i - 1] + candies[i] - x, candies[i]);
        candies[i] -= tmp;
        ans += tmp;
      } else {
        long long int tmp =
            min(candies[i - 1] + candies[i] - x, candies[i - 1]);
        candies[i - 1] -= tmp;
        ans += tmp;
      }
    }
  }
  if (candies[n - 2] + candies[n - 1] > x)
    ans += (candies[n - 2] + candies[n - 1]) - x;
  cout << ans << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
static int fast_io = []() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return 0;
}();
int main() {
  long long int n, x;
  cin >> n >> x;
  vector<long long int> candies(n);
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> candies[i];
  }
  for (int i = 1; i < n - 1; i++) {
    while (candies[i - 1] + candies[i] > x) {
      if (candies[i] > 0) {
        long long int tmp = min(candies[i - 1] + candies[i] - x, candies[i]);
        candies[i] -= tmp;
        ans += tmp;
      } else {
        long long int tmp =
            min(candies[i - 1] + candies[i] - x, candies[i - 1]);
        candies[i - 1] -= tmp;
        ans += tmp;
      }
    }
  }
  if (candies[n - 2] + candies[n - 1] > x)
    ans += (candies[n - 2] + candies[n - 1]) - x;
  cout << ans << "\n";
  return 0;
}
Problem p03862 (C++)

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

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;

#define INF 1 << 29
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (ll i = (k); i < (int)(n); i++)

vector<bool> gen_sosuu(vector<bool> sosuu) {
  int size = sosuu.size();
  REP(i, size) { sosuu[i] = true; }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i, 2, sqrt(size) + 1) {
    if (sosuu[i] == false)
      continue;
    for (int j = 2; i * j < size; j++) {
      sosuu[i * j] = false;
    }
  }
  return sosuu;
}

ll n, x;
ll a[10000];
ll sum = 0ll;
int main() {
  cin >> n >> x;
  REP(i, n) { cin >> a[i]; }
  if (a[0] > x) {
    sum += a[0] - x;
    a[0] = x;
  }
  FOR(i, 0, n - 1) {
    if (a[i] + a[i + 1] > x) {
      sum += a[i + 1] + a[i] - x;
      a[i + 1] = x - a[i];
    }
  }
  cout << sum << endl;
}


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

using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;

#define INF 1 << 29
#define REP(i, n) for (ll i = 0; i < (int)(n); i++)
#define FOR(i, k, n) for (ll i = (k); i < (int)(n); i++)

vector<bool> gen_sosuu(vector<bool> sosuu) {
  int size = sosuu.size();
  REP(i, size) { sosuu[i] = true; }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i, 2, sqrt(size) + 1) {
    if (sosuu[i] == false)
      continue;
    for (int j = 2; i * j < size; j++) {
      sosuu[i * j] = false;
    }
  }
  return sosuu;
}

ll n, x;
ll a[100000];
ll sum = 0ll;
int main() {
  cin >> n >> x;
  REP(i, n) { cin >> a[i]; }
  if (a[0] > x) {
    sum += a[0] - x;
    a[0] = x;
  }
  FOR(i, 0, n - 1) {
    if (a[i] + a[i + 1] > x) {
      sum += a[i + 1] + a[i] - x;
      a[i + 1] = x - a[i];
    }
  }
  cout << sum << endl;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
const ll mod = 1000000007;
const ll INF = 9223372036854775807;
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define Pi acos(-1)

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  ll n, x;
  cin >> n >> x;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n - 1) {
    while (a[i] + a[i + 1] > x) {
      if (a[i + 1] > 0) {
        --a[i + 1];
        ++ans;
      } else {
        --a[i];
        ++ans;
      }
    }
  }
  cout << ans << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
const ll mod = 1000000007;
const ll INF = 9223372036854775807;
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define Pi acos(-1)

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << setprecision(15) << fixed;
  ll n, x;
  cin >> n >> x;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n - 1) {
    ll t = a[i] + a[i + 1];
    if (t > x) {
      a[i + 1] -= t - x;
      ans += t - x;
      if (a[i + 1] < 0) {
        a[i + 1] = 0;
      }
    }
  }
  cout << ans << '\n';
}
Problem p03862 (C++)

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

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  ll ans = 0;
  REP(i, n) {
    if (i == 0)
      continue;
    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:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(v) (v).begin(), (v).end()
#define INF 2e9
typedef long long ll;

int main() {
  int n, x;
  cin >> n >> x;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  ll ans = 0;
  REP(i, n) {
    if (i == 0)
      continue;
    ll temp = a[i - 1] + a[i];
    if (temp > x) {
      a[i] = max(ll(0), a[i] - temp + x);
      ans += temp - x;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
long long inf = 100000000000007;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef pair<int, int> P;
string alph("abcdefghijklmnopqrstuvwxyz");
struct edge {
  int to, cost;
};
vector<edge> G[22];
vector<int> O[10];
P sg[10];
int d[1005][1005] = {};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
pair<int, double> s[1005];
int c = 0;
int fi[1005][1005];
int ans[1005][1005] = {};
bool graph[55][55];
bool visited[55];
int a[55], b[55];

signed main() {
  int n, m;
  cin >> n >> m;
  int num[100005];
  rep(i, n) { cin >> num[i]; }
  int ans = 0;
  if (num[0] > m) {
    ans += num[0] - m;
    num[0] = m;
  }
  for (int i = 1; i < n; i++) {
    while (num[i] + num[i - 1] > m) {
      num[i]--;
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;
long long inf = 100000000000007;
#define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef pair<int, int> P;
string alph("abcdefghijklmnopqrstuvwxyz");
struct edge {
  int to, cost;
};
vector<edge> G[22];
vector<int> O[10];
P sg[10];
int d[1005][1005] = {};
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
pair<int, double> s[1005];
int c = 0;
int fi[1005][1005];
int ans[1005][1005] = {};
bool graph[55][55];
bool visited[55];
int a[55], b[55];

signed main() {
  int n, m;
  cin >> n >> m;
  int num[100005];
  rep(i, n) { cin >> num[i]; }
  int ans = 0;
  if (num[0] > m) {
    ans += num[0] - m;
    num[0] = m;
  }
  for (int i = 1; i < n; i++) {
    int sum = num[i] + num[i - 1];
    if (sum <= m)
      continue;
    num[i] -= sum - m;
    ans += sum - m;
  }
  cout << ans << endl;
  return 0;
}
Problem p03862 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
#define ll long long
ll n, x;
ll a[111111];
ll ans, cnt;
int main() {
  cin >> n >> x;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    if (a[i] > x) {
      ans += a[i] - x;
      a[i] = x;
    }
  }

  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] - x;
      if (a[i + 1] < 0)
        while (true)
          ;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a) for (int i = 0; i < a; i++)
#define mp make_pair
#define pb push_back
#define P pair<int, int>
#define ll __int64
#define ll long long
ll n, x;
ll a[111111];
ll ans, cnt;
int main() {
  cin >> n >> x;
  rep(i, n) cin >> a[i];
  rep(i, n) {
    if (a[i] > x) {
      ans += a[i] - x;
      a[i] = x;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    if (a[i] + a[i + 1] > x) {
      ans += a[i] + a[i + 1] - x;
      ll sa = a[i] + a[i + 1] - x;
      if (sa > a[i + 1]) {
        a[i] = a[i] - (sa - a[i + 1]);
        a[i + 1] = 0;
      } else
        a[i + 1] -= sa;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03862 (C++)

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

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

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

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

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define cerr                                                                   \
  if (false)                                                                   \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;

void solve(long long N, long long x, vector<long long> a) {
  ll ans = 0;

  REP(i, N) {
    int buf = (a[i] - x > 0) ? a[i] - x : 0;
    ans += buf;
    a[i] -= buf;
  }

  FOR(i, 1, N) {
    ll buf = a[i - 1] + a[i];
    if (buf > x) {
      while (buf > x) {
        a[i]--;
        ans++;
        buf = a[i - 1] + a[i];
      }
    }
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  long long N;
  long long x;
  cin >> N;
  vector<long long> a(N - 1 + 1);
  cin >> x;
  for (int i = 0; i <= N - 1; i++) {
    cin >> a[i];
  }
  solve(N, x, a);
  return 0;
}


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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define POW(n) ((n) * (n))
#define ALL(a) (a).begin(), (a).end()
#define cerr                                                                   \
  if (false)                                                                   \
  cerr

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<unsigned long long> vull;

void solve(long long N, long long x, vector<long long> a) {
  ll ans = 0;

  REP(i, N) {
    int buf = (a[i] - x > 0) ? a[i] - x : 0;
    ans += buf;
    a[i] -= buf;
  }

  FOR(i, 1, N) {
    ll buf = a[i - 1] + a[i];
    if (buf > x) {
      // while(buf > x){
      //   a[i]--; ans++;
      //   buf = a[i-1] + a[i];
      // }
      ans += buf - x;
      a[i] -= buf - x;
    }
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  long long N;
  long long x;
  cin >> N;
  vector<long long> a(N - 1 + 1);
  cin >> x;
  for (int i = 0; i <= N - 1; i++) {
    cin >> a[i];
  }
  solve(N, x, a);
  return 0;
}
Problem p03862 (C++)

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

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999
#define MOD (int)(1e9) + 7

using namespace std;

int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};

int32_t main() {
  int n, x;
  cin >> n >> x;
  vector<int> v(n), a;
  REP(i, n) cin >> v[i];
  FOR(i, 1, n) a.pb(v[i - 1] + v[i]);

  int cnt = 0;
  REP(i, n - 1) {
    if (a[i] > x) {
      int y = a[i] - x;
      cnt += y;
      a[i] = x;
      if (i < n - 2) {
        for (int j = 0; (j < y && a[i + 1] > 0); j++) {
          a[i + 1]--;
        }
      }
    }
  }

  cout << cnt << endl;
}

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

#define int long long

#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define SORT(x) sort((x).begin(), (x).end())
#define INF 99999999999
#define MOD (int)(1e9) + 7

using namespace std;

int dy[4] = {1, 0, -1, 0};
int dx[4] = {0, 1, 0, -1};

int32_t main() {
  int n, x;
  cin >> n >> x;
  vector<int> v(n), a;
  REP(i, n) cin >> v[i];
  FOR(i, 1, n) a.pb(v[i - 1] + v[i]);

  int cnt = 0;
  REP(i, n - 1) {
    if (a[i] > x) {
      int y = a[i] - x;
      cnt += y;
      a[i] = x;
      if (i < n - 2) {
        if (v[i + 1] >= y)
          a[i + 1] -= y;
        else
          a[i + 1] = v[i + 2];
      }
    }
  }

  cout << cnt << endl;
}
Problem p03862 (C++)

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

using namespace std;

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

  int w[100000];
  for (int i = 0; i < n; i++)
    w[i] = a[i];

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

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

  cout << ans << endl;

  return 0;
}


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

using namespace std;

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

  int w[100000];
  for (int i = 0; i < n; i++)
    w[i] = a[i];

  for (int i = 0; i < n - 1; i++) {
    if (w[i] + w[i + 1] <= x)
      continue;
    if (w[i] > x)
      w[i] = x;
    w[i + 1] = x - w[i];
  }

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

  cout << ans << endl;

  return 0;
}
Problem p03863 (Python)

Buggy submission (status: Runtime Error):
s = input()
print("Second" if len(s) % 2 == 0 ^ s[0] != s[-1] else "First")


Fixed submission:
s = input()
print("Second" if (len(s) % 2 == 0) ^ (s[0] == s[-1]) else "First")
Problem p03863 (Python)

Buggy submission (status: Runtime Error):
from collections import Counter

s = input()

ans = ["Second", "First"]
c = Counter(s)
flag = 0 if len(c) == 2 else 1
same = 1 if s[0] == s[-1] else 0
print(ans[(len(s) % 2 + same) * flag])


Fixed submission:
from collections import Counter

s = input()

ans = ["Second", "First"]
c = Counter(s)
flag = 0 if len(c) == 2 else 1
same = 1 if s[0] == s[-1] else 0
print(ans[((len(s) + same) % 2) * flag])
Problem p03863 (C++)

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

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

string s;
void solve() {
  cin >> s;
  int n = s.size();

  if (s[0] == s[n - 1]) {
    assert(0);
    int cnt[26] = {};
    bool c[26] = {};
    for (int i = 1; i < n; i++) {
      if (s[i] == s[0]) {
        for (int j = 0; j < 26; j++) {
          cnt[j] += c[j];
          c[j] = 0;
        }
      } else {
        c[s[i] - 'a'] = 1;
      }
    }
    int ma = 0;
    for (int i = 0; i < 26; i++) {
      chmax(ma, cnt[i]);
    }
    if ((n - ma) & 1) {
      cout << "Second" << endl;
    } else {
      cout << "First" << endl;
    }
  } else {
    if (n & 1) {
      cout << "First" << endl;
    } else {
      cout << "Second" << endl;
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}

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

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64, i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

string s;
void solve() {
  cin >> s;
  int n = s.size();

  if (s[0] == s[n - 1]) {
    if (n & 1) {
      cout << "Second" << endl;
    } else {
      cout << "First" << endl;
    }
  } else {
    if (n & 1) {
      cout << "First" << endl;
    } else {
      cout << "Second" << endl;
    }
  }
}

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}
Problem p03863 (C++)

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

int main() {
  string s;
  cin >> s;
  int n = s.length();
  assert(n % 2 == 0);
  cout << "First" << endl;
}

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

int main() {
  string s;
  cin >> s;
  int n = s.size();
  bool a = (n % 2 == 0), b = (s[0] == s[n - 1]);
  if (a ^ b)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}
Problem p03863 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

int main() {
  string s;
  cin >> s;
  int num_op = 0;
  bool game_fin = false;
  while (!game_fin) {
    bool can_op = false;
    int i_e;
    for (int i = 1; i < s.size() - 1; i++) {
      if (s[i - 1] != s[i + 1]) {
        can_op = true;
        i_e = i;
      }
    }
    if (can_op) {
      s.erase(i_e, 1);
      num_op++;
    }
    if (s.size() == 2 || !can_op)
      game_fin = true;
  }
  if (num_op % 2 == 1) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  int num_op = 0;
  bool game_fin = false;
  while (!game_fin) {
    bool can_op = false;
    int i_e;
    for (int i = 1; i < s.size() - 1; i++) {
      if (s[i - 1] != s[i + 1]) {
        can_op = true;
        i_e = i;
        break;
      }
    }
    if (can_op) {
      s.erase(i_e, 1);
      num_op++;
    }
    if (s.size() == 2 || !can_op)
      game_fin = true;
  }
  if (num_op % 2 == 1) {
    cout << "First" << endl;
  } else {
    cout << "Second" << endl;
  }
  return 0;
}
Problem p03863 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  string s;
  cin >> s;
  int n = s.length(), cnt = 0;
  FOR(i, 1, n - 1) cnt += s[i - 1] != s[i + 1];
  if (cnt == 0) {
    cout << "Second\n";
    return 0;
  }
  if (cnt <= 3) {
    FOR(i, 1, n - 1) {
      if (s[i - 1] != s[i + 1]) {
        int sub = 1;
        sub += i >= 2 && s[i - 2] == s[i + 1] ? 1 : -1;
        sub += i + 2 < n && s[i - 1] == s[i + 2] ? 1 : -1;
        if (cnt == sub) {
          cout << "First\n";
          return 0;
        }
      }
    }
  }
  assert(n % 2 == 0);
  cout << (n & 1 ? "First\n" : "Second\n");
  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1},
          dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) {
  return a < b ? (a = b, true) : false;
}
template <typename T, typename U> inline bool chmin(T &a, U b) {
  return a > b ? (a = b, true) : false;
}
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  string s;
  cin >> s;
  cout << ((s.length() % 2 == 0) ^ (s.front() == s.back()) ? "Second\n"
                                                           : "First\n");
  return 0;
}
Problem p03863 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <bitset>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
list<char> theList;
const int maxn = 1e5 + 20;
char str[maxn];
void work() {
  scanf("%s", str + 1);
  int lenstr = strlen(str + 1);
  for (int i = 1; i <= lenstr; ++i) {
    theList.push_back(str[i]);
  }
  //    for (list<char> :: iterator it = theList.begin(); it != theList.end();
  //    ++it) {
  //        cout << *it;
  //    }
  int ans = 0;
  while (theList.size() > 2) {
    if (theList.size() == 3 && theList.front() == theList.back())
      break;
    list<char>::iterator itBegin = theList.begin();
    itBegin++;
    list<char>::iterator itEnd = theList.end();
    itEnd--;
    list<char>::iterator itBeginPre = theList.begin();
    list<char>::iterator itBeginNext = itBegin;
    itBeginNext++;
    bool flag = false;
    while (itBegin != itEnd) {
      if (*itBeginPre == *itBeginNext) {
        itBeginPre++;
        itBegin++;
        itBeginNext++;
      } else {
        theList.erase(itBegin);
        flag = true;
        break;
      }
    }
    if (!flag)
      break;
    ans++;
  }
  //    cout << ans << endl;
  if (ans & 1) {
    cout << "First" << endl;
  } else
    cout << "Second" << endl;
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <bitset>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
list<char> theList;
const int maxn = 1e5 + 20;
char str[maxn];
void work() {
  scanf("%s", str + 1);
  int lenstr = strlen(str + 1);
  lenstr -= 2;
  if (str[1] == str[strlen(str + 1)])
    lenstr--;
  if (lenstr & 1) {
    cout << "First" << endl;
  } else
    cout << "Second" << endl;
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}
Problem p03863 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> cnt(26);
  rep(i, n) cnt[s[i] - 'a']++;
  sort(cnt.begin(), cnt.end(), greater<ll>());
  ll tmp = 0;
  for (ll i = 3; i < n; i++)
    tmp += cnt[i];
  tmp += cnt[2] - 2;
  if (cnt[2] == 0)
    cout << "Second" << endl;
  else if (tmp % 2 == 0)
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}

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

int main() {
  string s;
  cin >> s;
  ll n = s.size();
  vector<ll> cnt(26, 0);
  if ((n % 2 == 0) ^ (s[0] == s[n - 1]))
    cout << "Second" << endl;
  else
    cout << "First" << endl;
  return 0;
}
Problem p03863 (C++)

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

int main() {
  string s;
  cin >> s;
  string ans[2] = {"First", "Second"};

  int idx = 1;
  int len = s.size();
  while (len > 2) {
    for (int i = 1; i < len - 1; i++) {
      if (s[i - 1] != s[i + 1]) {
        s.erase(i, 1);
        if (idx) {
          idx = 0;
        } else {
          idx = 1;
        }
        break;
      }
    }
    len--;
  }

  cout << ans[idx] << endl;
  return 0;
}


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

int main() {
  string s;
  cin >> s;
  string ans[2] = {"First", "Second"};

  int idx = 1;
  int len = s.size();
  while (len > 2) {
    for (int i = 1; i < len - 1; i++) {
      if (s[i - 1] != s[i + 1]) {
        s.erase(i, 1);
        if (idx) {
          idx = 0;
        } else {
          idx = 1;
        }
        break;
      }
      if (i == len - 2) {
        cout << ans[idx] << endl;
        return 0;
      }
    }
    len--;
  }

  cout << ans[idx] << endl;
  return 0;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

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


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define ub upper_bound
#define lb lower_bound
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

signed main() {
  int n, x;
  cin >> n >> x;
  vector<int> a(n), b;
  rep(i, n) cin >> a[i];
  int ans = 0;
  if (a[0] > x) {
    ans += a[0] - x;
    a[0] = x;
  }
  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);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n, x;
  long long count = 0;
  cin >> n >> x;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    while (a[i] + a[i + 1] > x) {
      a[i + 1]--;
      count++;
    }
  }
  cout << count << endl;
}

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

int main() {
  int n, x;
  long long count = 0;
  cin >> n >> x;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    long long y = a[i + 1] + a[i] - x;
    if (y > 0) {
      count += y;
      if (a[i + 1] >= y)
        a[i + 1] -= y;
      else
        a[i + 1] = 0;
    }
  }
  cout << count << endl;
}
Problem p03864 (C++)

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

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

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

int main() {
  long long int N, x, a[100000], count = 0;
  cin >> N >> x;
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 1; i < N; i++) {
    if (a[i - 1] > x) {
      count += a[i - 1] + a[i] - x;
      a[i] = 0;
    } else if (a[i - 1] + a[i] > x) {
      count += a[i - 1] + a[i] - x;
      a[i] = x - a[i - 1];
    }
  }
  cout << count;
}
Problem p03864 (C++)

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

#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
typedef long long int ll;

int main() {
  ios::sync_with_stdio(false);
  ll n, x;
  cin >> n >> x;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  ll ans = 0;
  // fisrt and end
  if (a.at(0) - x > 0) {
    ans += a.at(0) - x;
    a.at(0) = x;
  }
  if (a.at(n - 1) - x > 0) {
    ans += a.at(n - 1) - x;
    a.at(n - 1) = x;
  }

  // ruisekiwa
  vector<ll> sums(n - 1);
  rep(i, n - 1) sums.at(i) = a.at(i) + a.at(i + 1);

  for (int j = 0; j < n - 1; ++j) {
    if (sums.at(j) > x) {
      ans += sums.at(j) - x;
      sums.at(j + 1) -= (sums.at(j) - x);
    }
  }

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

Fixed submission:
#include <cmath>
#include <cstdio>

#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
typedef long long int ll;

int main() {
  ios::sync_with_stdio(false);
  ll n, x;
  cin >> n >> x;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);

  ll ans = 0;
  // fisrt and end
  if (a.at(0) - x > 0) {
    ans += a.at(0) - x;
    a.at(0) = x;
  }
  if (a.at(n - 1) - x > 0) {
    ans += a.at(n - 1) - x;
    a.at(n - 1) = x;
  }

  // ruisekiwa
  vector<ll> sums(n - 1);
  rep(i, n - 1) sums.at(i) = a.at(i) + a.at(i + 1);

  for (int j = 0; j < n - 1; ++j) {
    if (sums.at(j) > x) {
      ans += sums.at(j) - x;
      if (j != n - 2)
        sums.at(j + 1) -= (sums.at(j) - x);
    }
  }

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

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

int main() {
  int n;
  ll x;
  cin >> n >> x;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll cnt = 0;
  for (int i = 0; i < n; i += 2) {
    if (a[i] > x) {
      cnt += a[i] - x;
      a[i] -= a[i] - x;
      // cout << a[i] << endl;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i + 1 > n)
      continue;
    if (a[i] + a[i + 1] > x) {
      cnt += a[i] + a[i + 1] - x;
      a[i + 1] = x - a[i];
    }
  }
  cout << cnt << endl;
}


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

int main() {
  int n;
  ll x;
  cin >> n >> x;
  vector<ll> a(n + 10);
  rep(i, n) cin >> a[i];
  ll cnt = 0;
  for (int i = 0; i < n; i += 2) {
    if (a[i] > x) {
      cnt += a[i] - x;
      a[i] -= a[i] - x;
      // cout << a[i] << endl;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i + 1 > n)
      continue;
    if (a[i] + a[i + 1] > x) {
      cnt += a[i] + a[i + 1] - x;
      a[i + 1] = x - a[i];
    }
  }
  cout << cnt << endl;
}
Problem p03864 (C++)

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

using namespace std;

int main(void) {
  int N, x;
  long long int a[10000], b[10000] = {};

  cin >> N >> x;

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

  for (int i = 0; i < N - 1; ++i) {
    b[i] = a[i] + a[i + 1] - x;
  }

  unsigned long long int result = 0;

  for (int i = 0; i < N - 1; ++i) {
    if (b[i] > 0) {
      result += b[i];
      // cout <<"b["<< i <<"]:"<< b[i]<<"\n"
      //      <<"result:"<< result<< "\n";
      b[i + 1] -= (b[i] < a[i + 1] ? b[i] : a[i + 1]);
    }
  }

  cout << result;

  return 0;
}


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

using namespace std;

int main(void) {
  int N, x;
  long long int a[100000], b[100000] = {};

  cin >> N >> x;

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

  for (int i = 0; i < N - 1; ++i) {
    b[i] = a[i] + a[i + 1] - x;
  }

  unsigned long long int result = 0;

  for (int i = 0; i < N - 1; ++i) {
    if (b[i] > 0) {
      result += b[i];
      // cout <<"b["<< i <<"]:"<< b[i]<<"\n"
      //      <<"result:"<< result<< "\n";
      b[i + 1] -= (b[i] < a[i + 1] ? b[i] : a[i + 1]);
    }
  }

  cout << result;

  return 0;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define MAXS 100000;
#define p pair<int, int>
using namespace std;
int main(int argc, char const *argv[]) {
  int n, x;
  cin >> n >> x;
  vector<long long> a(n, 0);
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    while (a[i] + a[i + 1] > x) {
      if (a[i + 1] >= 0) {
        a[i + 1]--;
        count++;
      } else {
        a[i]--;
        count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define MAXS 100000;
#define p pair<int, int>
using namespace std;
int main(int argc, char const *argv[]) {
  int n, x;
  cin >> n >> x;
  vector<long long> a(n, 0);
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    while (a[i] + a[i + 1] > x) {
      if (a[i + 1] >= 0 && a[i + 1] >= a[i] + a[i + 1] - x) {
        count += (a[i] + a[i + 1] - x);
        a[i + 1] -= (a[i] + a[i + 1] - x);
      } else if (a[i + 1] >= 0 && a[i + 1] < a[i] + a[i + 1] - x) {
        count += a[i + 1];
        count += (a[i] - x);
        a[i] -= (a[i] - x);
        a[i + 1] = 0;
      }
    }
  }
  cout << count << endl;
  return 0;
}
Problem p03864 (C++)

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

int main() {
  long long n, x, a[10000];
  cin >> n >> x;

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

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

  cout << ans << endl;
}


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

int main() {
  long long n, x, a[100000];
  cin >> n >> x;

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

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

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
const long double pi = 3.14159265358979323846264338327950288;
int n;
ll x, a[100000];
int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  for (int i = 1; i < n; i++) {
    while (a[i - 1] + a[i] > x) {
      a[i]--;
      sum++;
    }
  }
  cout << sum << endl;
  cin >> sum;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
const long double pi = 3.14159265358979323846264338327950288;
int n;
ll x, a[100000];
int main() {
  cin >> n >> x;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll sum = 0;
  for (int i = 1; i < n; i++) {
    if (a[i - 1] + a[i] > x) {
      sum += min(a[i - 1] + a[i] - x, a[i]);
      a[i] -= min(a[i - 1] + a[i] - x, a[i]);
    }
    if (a[i - 1] + a[i] > x) {
      sum += (a[i - 1] + a[i] - x);
      a[i - 1] -= (a[i - 1] + a[i] - x);
    }
  }
  cout << sum << endl;
  cin >> sum;
}
Problem p03864 (C++)

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

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

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

using namespace std;

// const long long int mod = 1e9+7;
const long long int MAXN = 1e5;

int main() {
  long long int n, a[MAXN] = {0}, x;

  cin >> n >> x;

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

  long long int cnt = 0;

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

  cout << cnt << endl;

  return 0;
}


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

using namespace std;

// const long long int mod = 1e9+7;
const long long int MAXN = 1e5;

int main() {
  long long int n, a[MAXN] = {0}, x;

  cin >> n >> x;

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

  long long int cnt = 0;

  for (int i = 1; i < n; i++) {
    if (a[i - 1] + a[i] > x) {
      long long int s = a[i - 1] + a[i] - x;
      if (a[i] - s < 0) {
        a[i] = 0;
        a[i - 1] -= s - a[i];
        cnt += s;
      } else {
        a[i] -= s;
        cnt += s;
      }
    }
  }

  cout << cnt << endl;

  return 0;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
typedef pair<int, int> P;
typedef pair<int, P> PP;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define pb(x) push_back(x)
#define fi first
#define se second
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
int mod = 1e9 + 7;

int a[100010];
signed main() {
  int n, x;
  scanf("%lld%lld", &n, &x);
  rep(i, n) scanf("%lld", &a[i]);
  int ans = 0;
  rep(i, n) {
    while (a[i] + a[i + 1] > x) {
      a[i + 1]--;
      ans++;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define repi(i, m, n) for (int i = m; i < n; i++)
#define drep(i, n, m) for (int i = n; i >= m; i--)
#define rep(i, n) repi(i, 0, n)
#define rrep(i, n) repi(i, 1, n + 1)
typedef pair<int, int> P;
typedef pair<int, P> PP;
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define pb(x) push_back(x)
#define fi first
#define se second
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
int mod = 1e9 + 7;

int a[100010];
signed main() {
  int n, x;
  scanf("%lld%lld", &n, &x);
  rep(i, n) scanf("%lld", &a[i]);
  int ans = 0;
  rep(i, n) {
    if (a[i] + a[i + 1] > x) {
      int d = a[i] + a[i + 1] - x;
      ans += d;
      a[i + 1] -= d;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, x, i, j, k, base = 0, ans = 0;
  ll dp[2] = {};
  cin >> n >> x;
  ll a[n + 2], b[n + 2];
  a[0] = a[n + 1] = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > x)
      base += a[i] - x, a[i] = x;
  }
  for (i = 1; i <= n; i++) {
    if (!a[i])
      ans += min(dp[0], dp[1]), dp[0] = dp[1] = 0;
    else
      dp[i % 2] += max(0LL, a[i] - min(x - a[i - 1], x - a[i + 1]));
  }
  ans += min(dp[0], dp[1]);
  for (i = 0; i <= n + 1; i++)
    b[i] = a[i];
  k = 0;
  for (i = 1; i <= n; i++) {
    if (b[i] + b[i + 1] > x)
      j = b[i] + b[i + 1] - x, b[i + 1] -= j, k += j;
  }
  ans = min(ans, k);
  for (i = 0; i <= n + 1; i++)
    b[i] = a[i];
  k = 0;
  for (i = n; i >= 1; i++) {
    if (b[i] + b[i - 1] > x)
      j = b[i] + b[i - 1] - x, b[i - 1] -= j, k += j;
  }
  ans = min(ans, k);
  cout << base + ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, x, i, j, k, base = 0, ans = 0;
  ll dp[2] = {};
  cin >> n >> x;
  ll a[n + 2], b[n + 2];
  a[0] = a[n + 1] = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] > x)
      base += a[i] - x, a[i] = x;
  }
  for (i = 1; i <= n; i++) {
    if (!a[i])
      ans += min(dp[0], dp[1]), dp[0] = dp[1] = 0;
    else
      dp[i % 2] += max(0LL, a[i] - min(x - a[i - 1], x - a[i + 1]));
  }
  ans += min(dp[0], dp[1]);
  for (i = 0; i <= n + 1; i++)
    b[i] = a[i];
  k = 0;
  for (i = 1; i <= n; i++) {
    if (b[i] + b[i + 1] > x)
      j = b[i] + b[i + 1] - x, b[i + 1] -= j, k += j;
  }
  ans = min(ans, k);
  for (i = 0; i <= n + 1; i++)
    b[i] = a[i];
  k = 0;
  for (i = n; i >= 1; i--) {
    if (b[i] + b[i - 1] > x)
      j = b[i] + b[i - 1] - x, b[i - 1] -= j, k += j;
  }
  ans = min(ans, k);
  cout << base + ans << endl;
  return 0;
}
Problem p03864 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint n, x;
  cin >> n >> x;
  vector<lint> a(n);
  REP(i, n) cin >> a[i];
  lint ans = 0;
  REP(i, n - 1) {
    while (a[i + 1] + a[i] > x) {
      if (a[i + 1] != 0) {
        a[i + 1]--;
        ans++;
      } else if (a[i] != 0) {
        a[i]--;
        ans++;
      }
    }
  }
  print(ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define print(s) cout << (s) << endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint n, x;
  cin >> n >> x;
  vector<lint> a(n);
  REP(i, n) cin >> a[i];
  lint ans = 0;
  REP(i, n - 1) {
    if (a[i + 1] + a[i] > x) {
      if (a[i] > x) {
        ans += a[i + 1];
        a[i + 1] = 0;
        ans += a[i] - x;
        a[i] -= x;
      } else {
        ans += a[i + 1] + a[i] - x;
        a[i + 1] -= a[i + 1] + a[i] - x;
      }
    }
  }
  print(ans);
  return 0;
}