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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  scanf("%d", &n);
  std::string s;
  cin >> s;
  int q;
  scanf("%d", &q);

  std::vector<i64> sum(n + 1, 0);
  std::vector<int> latte;
  for (int i = 0; i < n; i++)
    sum[i + 1] = sum[i] + (s[i] == 'M');
  for (int i = 0; i < n; i++)
    if (s[i] == 'D')
      latte.push_back(i);

  while (q--) {
    int k;
    scanf("%d", &k);

    i64 ans = 0, tmp = 0, c = 0, size = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'D') {
        tmp += sum[i + 1];
        size++;
      } else if (s[i] == 'C') {
        while (latte[c] < i and i - latte[c] >= k) {
          tmp -= sum[latte[c++] + 1];
          size--;
        }

        ans += size * sum[i + 1] - tmp;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long i64;
using std::cin;
using std::cout;
using std::endl;

int main() {
  int n;
  scanf("%d", &n);
  std::string s;
  cin >> s;
  int q;
  scanf("%d", &q);

  std::vector<i64> sum(n + 1, 0);
  std::vector<int> latte;
  for (int i = 0; i < n; i++)
    sum[i + 1] = sum[i] + (s[i] == 'M');
  for (int i = 0; i < n; i++)
    if (s[i] == 'D')
      latte.push_back(i);

  while (q--) {
    int k;
    scanf("%d", &k);

    i64 ans = 0, tmp = 0, c = 0, size = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'D') {
        tmp += sum[i + 1];
        size++;
      } else if (s[i] == 'C') {
        while (c < latte.size() and latte[c] < i and i - latte[c] >= k) {
          tmp -= sum[latte[c++] + 1];
          size--;
        }

        ans += size * sum[i + 1] - tmp;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

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;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    ll d = 0, m = 0, dm = 0, dmc = 0;
    rep(i, n) {
      if (i < k) {
        if (s[i - k] == 'D') {
          d--;
          dm -= m;
        } else if (s[i - k] == 'M')
          m--;
      }
      if (s[i] == 'D')
        d++;
      else if (s[i] == 'M') {
        dm += d;
        m++;
      } else if (s[i] == 'C')
        dmc += dm;
    }
    cout << dmc << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i >= 0; i--)
#define loop(i, r, n) for (int i = (r); i < (n); i++)
#define pb push_back
#define all(in) in.begin(), in.end()

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;
}

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;
    ll d = 0, m = 0, dm = 0, dmc = 0;
    rep(i, n) {
      if (i >= k) {
        if (s[i - k] == 'D') {
          d--;
          dm -= m;
        } else if (s[i - k] == 'M')
          m--;
      }
      if (s[i] == 'D')
        d++;
      else if (s[i] == 'M') {
        dm += d;
        m++;
      } else if (s[i] == 'C')
        dmc += dm;
    }
    cout << dmc << endl;
  }
  return 0;
}
Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Int long long
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int U;
  cin >> U;
  while (U--) {
    Int ans = 0, res = 0, add = 0;
    int tmp;
    cin >> tmp;
    tmp--;
    queue<Int> q;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'D') {
        q.push(add);
      } else if (s[i] == 'M') {
        res += q.size();
        add++;
      } else if (s[i] == 'C') {
        ans += res;
      }
      if (i >= 1) {
        int k = i - tmp;
        if (s[k] == 'D') {
          res -= (add - q.front());
          q.pop();
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define Int long long
using namespace std;
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int U;
  cin >> U;
  while (U--) {
    Int ans = 0, res = 0, add = 0;
    int tmp;
    cin >> tmp;
    tmp--;
    queue<Int> q;
    for (int i = 0; i < n; i++) {
      if (s[i] == 'D') {
        q.push(add);
      } else if (s[i] == 'M') {
        res += q.size();
        add++;
      } else if (s[i] == 'C') {
        ans += res;
      }
      if (i >= tmp) {
        int k = i - tmp;
        if (s[k] == 'D') {
          res -= (add - q.front());
          q.pop();
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03216 (C++)

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

#define int long long

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;

  string S;
  cin >> S;

  vector<int> cumb(2 * N + 1, 0), cumc(2 * N + 1, 0), cumbb(2 * N + 1, 0);
  for (int i = 0; i < 2 * N + 1; i++) {
    if (i) {
      cumb[i] = cumb[i - 1];
      cumc[i] = cumc[i - 1];
      cumbb[i] = cumbb[i - 1];
    }
    if (S[i] == 'M') {
      cumb[i]++;
    }
    if (S[i] == 'C') {
      cumc[i]++;
      cumbb[i] += cumb[i];
    }
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int k;
    cin >> k;
    int ans = 0;
    for (int j = 0; j < N; j++) {
      if (S[j] == 'D') {
        ans +=
            cumbb[j + k - 1] - (cumc[j + k - 1] - cumc[j]) * cumb[j] - cumbb[j];
        // cout << cumbb[j+k] << endl;
      }
    }

    cout << ans << endl;
  }

  return 0;
}


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

#define int long long

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N;
  cin >> N;

  string S;
  cin >> S;

  vector<int> cumb(2 * N + 1, 0), cumc(2 * N + 1, 0), cumbb(2 * N + 1, 0);
  for (int i = 0; i < 2 * N + 1; i++) {
    if (i) {
      cumb[i] = cumb[i - 1];
      cumc[i] = cumc[i - 1];
      cumbb[i] = cumbb[i - 1];
    }
    if (i >= N)
      continue;
    if (S[i] == 'M') {
      cumb[i]++;
    }
    if (S[i] == 'C') {
      cumc[i]++;
      cumbb[i] += cumb[i];
    }
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int k;
    cin >> k;
    int ans = 0;
    for (int j = 0; j < N; j++) {
      if (S[j] == 'D') {
        ans +=
            cumbb[j + k - 1] - (cumc[j + k - 1] - cumc[j]) * cumb[j] - cumbb[j];
        // cout << cumbb[j+k] << endl;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p03216 (C++)

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

using namespace std;

long d[1111111], m[1111111], dm[1111111];
int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i < n; i++)
    d[i + 1] = d[i] + (s[i] == 'D'), m[i + 1] = m[i] + (s[i] == 'M'),
          dm[i + 1] = dm[i] + d[i] * (s[i] == 'M');
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k;
    k--;
    long r = 0;
    for (int j = 2; j < n; j++) {
      if (s[j] != 'C')
        continue;
      r += dm[j] - dm[j - k] - d[j - k] * (m[j] - m[j - k]);
    }
    cout << r << '\n';
  }
  cout << flush;
  return 0;
}


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

using namespace std;

long d[1111111], m[1111111], dm[1111111];
int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i < n; i++)
    d[i + 1] = d[i] + (s[i] == 'D'), m[i + 1] = m[i] + (s[i] == 'M'),
          dm[i + 1] = dm[i] + d[i] * (s[i] == 'M');
  for (int i = 0; i < q; i++) {
    int k;
    cin >> k;
    k--;
    long r = 0;
    for (int j = 2; j < n; j++) {
      if (s[j] != 'C')
        continue;
      int l = max(0, j - k);
      r += dm[j] - dm[l] - d[l] * (m[j] - m[l]);
    }
    cout << r << '\n';
  }
  cout << flush;
  return 0;
}
Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll q;
  cin >> q;
  vector<ll> d, m, c;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'D')
      d.push_back(i);
    if (s[i] == 'M')
      m.push_back(i);
    if (s[i] == 'C')
      c.push_back(i);
  }
  ll sum[n + 1];
  sum[0] = 0;
  for (ll i = 0; i < c.size(); i++) {
    ll y = lower_bound(m.begin(), m.end(), c[i]) - m.begin();
    sum[i + 1] = sum[i] + y;
  }
  ll dm[200010], dc[200010];
  for (ll i = 0; i < 200001; i++) {
    ll mm = lower_bound(m.begin(), m.end(), i) - m.begin();
    ll cc = lower_bound(c.begin(), c.end(), i) - c.begin();
    dm[i] = mm;
    dc[i] = cc;
  }
  for (ll i = 0; i < q; i++) {
    ll k;
    cin >> k;
    ll ans = 0;
    for (ll j = 0; j < d.size(); j++) {
      ll rc = dc[d[j] + k], lc = dc[d[j]], lm = dm[d[j]];
      ans += (sum[rc] - sum[lc]) - (rc - lc) * lm;
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll q;
  cin >> q;
  vector<ll> d, m, c;
  for (ll i = 0; i < n; i++) {
    if (s[i] == 'D')
      d.push_back(i);
    if (s[i] == 'M')
      m.push_back(i);
    if (s[i] == 'C')
      c.push_back(i);
  }
  ll sum[n + 1];
  sum[0] = 0;
  for (ll i = 0; i < c.size(); i++) {
    ll y = lower_bound(m.begin(), m.end(), c[i]) - m.begin();
    sum[i + 1] = sum[i] + y;
  }
  ll dm[2000010], dc[2000010];
  for (ll i = 0; i < 2000001; i++) {
    ll mm = lower_bound(m.begin(), m.end(), i) - m.begin();
    ll cc = lower_bound(c.begin(), c.end(), i) - c.begin();
    dm[i] = mm;
    dc[i] = cc;
  }
  for (ll i = 0; i < q; i++) {
    ll k;
    cin >> k;
    ll ans = 0;
    for (ll j = 0; j < d.size(); j++) {
      ll rc = dc[d[j] + k], lc = dc[d[j]], lm = dm[d[j]];
      ans += (sum[rc] - sum[lc]) - (rc - lc) * lm;
    }
    cout << ans << endl;
  }
}
Problem p03216 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

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

  ll N, Q;
  string S;
  cin >> N >> S >> Q;

  while (Q--) {
    ll k;
    cin >> k;
    ll d = 0, dm = 0, m = 0, ans = 0;
    Rep(i, k) {
      if (S[i] == 'D')
        d++;
      if (S[i] == 'M') {
        dm += d;
        m++;
      }
      if (S[i] == 'C') {
        ans += dm;
      }
      cerr << i << " " << d << " " << m << " " << dm << "\n";
    }

    Rep(i, N - k) {
      if (S[i] == 'D') {
        d--;
        dm -= m;
      }
      if (S[i] == 'M') {
        m--;
      }

      if (S[i + k] == 'D')
        d++;
      if (S[i + k] == 'M') {
        dm += d;
        m++;
      }
      if (S[i + k] == 'C') {
        ans += dm;
      }
      // cout << i << " " << d << " " << m << " " << dm << "\n";
    }

    cout << ans << "\n";
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define ll int64_t
#define Rep(i, n) for (ll i = 0; i < n; i++)
using namespace std;
typedef vector<ll> vec;
typedef vector<vec> mat;

const ll inf = 1LL << 60;

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

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

  ll N, Q;
  string S;
  cin >> N >> S >> Q;

  while (Q--) {
    ll k;
    cin >> k;
    ll d = 0, dm = 0, m = 0, ans = 0;
    Rep(i, k) {
      if (S[i] == 'D')
        d++;
      if (S[i] == 'M') {
        dm += d;
        m++;
      }
      if (S[i] == 'C') {
        ans += dm;
      }
      // cerr << i << " " << d << " " << m << " " << dm << "\n";
    }

    Rep(i, N - k) {
      if (S[i] == 'D') {
        d--;
        dm -= m;
      }
      if (S[i] == 'M') {
        m--;
      }

      if (S[i + k] == 'D')
        d++;
      if (S[i + k] == 'M') {
        dm += d;
        m++;
      }
      if (S[i + k] == 'C') {
        ans += dm;
      }
      // cout << i << " " << d << " " << m << " " << dm << "\n";
    }

    cout << ans << "\n";
  }
}
Problem p03216 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  int N;
  string S;
  cin >> N >> S;
  // f : 場所
  // s : その場所より前にある'M'の個数
  vector<int> Df;
  vector<int> Cf;
  vector<int> Ds;
  vector<int> Cs;
  int Mcnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'D') {
      Df.push_back(i);
      Ds.push_back(Mcnt);
    } else if (S[i] == 'C') {
      Cf.push_back(i);
      Cs.push_back(Mcnt);
    } else if (S[i] == 'M') {
      Mcnt++;
    }
  }

  int Dsz = Df.size();
  int Csz = Cf.size();
  BIT<ll> bit(Csz);

  for (int i = 0; i < Csz; i++) {
    bit.add(i, Cs[i]);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int k;
    cin >> k;
    ll ans = 0;
    for (int i = 0; i < Dsz; i++) {
      ll l = lower_bound(Cf.begin(), Cf.end(), Df[i]) - Cf.begin();
      ll r = lower_bound(Cf.begin(), Cf.end(), Df[i] + k) - Cf.begin() - 1;
      ll res = bit.sum(l, r + 1);
      res -= Ds[i] * (r - l + 1);
      ans += res;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  int N;
  string S;
  cin >> N >> S;
  // f : 場所
  // s : その場所より前にある'M'の個数
  vector<int> Df;
  vector<int> Cf;
  vector<int> Ds;
  vector<int> Cs;
  int Mcnt = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'D') {
      Df.push_back(i);
      Ds.push_back(Mcnt);
    } else if (S[i] == 'C') {
      Cf.push_back(i);
      Cs.push_back(Mcnt);
    } else if (S[i] == 'M') {
      Mcnt++;
    }
  }

  int Dsz = Df.size();
  int Csz = Cf.size();
  BIT<ll> bit(Csz);

  for (int i = 0; i < Csz; i++) {
    bit.add(i, Cs[i]);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int k;
    cin >> k;
    ll ans = 0;
    for (int i = 0; i < Dsz; i++) {
      ll l = lower_bound(Cf.begin(), Cf.end(), Df[i]) - Cf.begin();
      ll r = lower_bound(Cf.begin(), Cf.end(), Df[i] + k) - Cf.begin() - 1;
      ll res = bit.sum(l, r + 1);
      res -= Ds[i] * (r - l + 1);
      ans += res;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll MOD = 1e9 + 7;

int main() {
  ll n, q;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i < 100; i++)
    s += '.';
  for (int cn = 0; cn < q; cn++) {
    ll k, ans = 0;
    cin >> k;
    ll cntc = 0, sumn = 0, cntm = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'M')
        cntm++;
      if (s[i] == 'C') {
        sumn += cntm;
        cntc++;
      }
    }
    for (int i = 0; s[i] != '.'; i++) {
      if (s[i] == 'D')
        ans += sumn;
      if (s[i] == 'M') {
        cntm--;
        sumn -= cntc;
      }
      if (s[i] == 'C') {
        cntc--;
      }
      if (s[i + k] == 'M')
        cntm++;
      if (s[i + k] == 'C') {
        cntc++;
        sumn += cntm;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll, ll> P;
typedef pair<ll, P> PP;
const ll MOD = 1e9 + 7;

int main() {
  ll n, q;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i <= n; i++)
    s += '.';
  for (int cn = 0; cn < q; cn++) {
    ll k, ans = 0;
    cin >> k;
    ll cntc = 0, sumn = 0, cntm = 0;
    for (int i = 0; i < k; i++) {
      if (s[i] == 'M')
        cntm++;
      if (s[i] == 'C') {
        sumn += cntm;
        cntc++;
      }
    }
    for (int i = 0; s[i] != '.'; i++) {
      if (s[i] == 'D')
        ans += sumn;
      if (s[i] == 'M') {
        cntm--;
        sumn -= cntc;
      }
      if (s[i] == 'C') {
        cntc--;
      }
      if (s[i + k] == 'M')
        cntm++;
      if (s[i + k] == 'C') {
        cntc++;
        sumn += cntm;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  int N;
  string S;
  cin >> N >> S;
  int Q;
  cin >> Q;
  while (Q--) {
    int K;
    scanf("%d", &K);

    ll ans = 0;
    ll cntm = 0, cntc = 0, cur = 0, pos = 0;
    REP(i, N) {
      while (pos < i + K) {
        if (S[pos] == 'M')
          ++cntm;
        else if (S[pos] == 'C') {
          ++cntc;
          cur += cntm;
        }
        ++pos;
      }
      if (S[i] == 'D')
        ans += cur;
      else if (S[i] == 'M') {
        --cntm;
        cur -= cntc;
      } else if (S[i] == 'C') {
        --cntc;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define fi first
#define se second
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll << 30;
constexpr ll longINF = 1ll << 60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//

int main() {
  int N;
  string S;
  cin >> N >> S;
  int Q;
  cin >> Q;
  while (Q--) {
    int K;
    scanf("%d", &K);

    ll ans = 0;
    ll cntm = 0, cntc = 0, cur = 0, pos = 0;
    REP(i, N) {
      while (pos < N && pos < i + K) {
        if (S[pos] == 'M')
          ++cntm;
        else if (S[pos] == 'C') {
          ++cntc;
          cur += cntm;
        }
        ++pos;
      }
      if (S[i] == 'D')
        ans += cur;
      else if (S[i] == 'M') {
        --cntm;
        cur -= cntc;
      } else if (S[i] == 'C') {
        --cntc;
      }
    }
    printf("%lld\n", ans);
  }

  return 0;
}
Problem p03216 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define maxn 1000005
#define ll long long
#define ld double
#define mod 998244353
using namespace std;
char inp[maxn];
struct th {
  ll a[maxn];
  th() { memset(a, 0, sizeof(a)); }
  void add(int pl, int num) { a[pl] += num; }
  void build() {
    for (int i = 1; i < maxn; i++)
      a[i] += a[i - 1];
  }
  ll q(int l, int r) { return a[r] - a[l - 1]; }
} q1, q2;
int k[maxn];
int sum[maxn];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", inp + 1);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1];
    if (inp[i] == 'M')
      sum[i]++;
    if (inp[i] == 'C')
      q1.add(i, 1), q2.add(i, sum[i]);
  }
  q1.build(), q2.build();
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int k;
    scanf("%d", &k);
    ll ans = 0;
    for (int j = 1; j <= n; j++)
      if (inp[j] == 'D')
        ans += q2.q(j, j + k - 1) - sum[j] * q1.q(j, j + k - 1);
    printf("%lld\n", ans);
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define maxn 2000005
#define ll long long
#define ld double
#define mod 998244353
using namespace std;
char inp[maxn];
struct th {
  ll a[maxn];
  th() { memset(a, 0, sizeof(a)); }
  void add(int pl, int num) { a[pl] += num; }
  void build() {
    for (int i = 1; i < maxn; i++)
      a[i] += a[i - 1];
  }
  ll q(int l, int r) { return a[r] - a[l - 1]; }
} q1, q2;
int k[maxn];
int sum[maxn];
int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", inp + 1);
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i - 1];
    if (inp[i] == 'M')
      sum[i]++;
    if (inp[i] == 'C')
      q1.add(i, 1), q2.add(i, sum[i]);
  }
  q1.build(), q2.build();
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int k;
    scanf("%d", &k);
    ll ans = 0;
    for (int j = 1; j <= n; j++)
      if (inp[j] == 'D')
        ans += q2.q(j, j + k - 1) - sum[j] * q1.q(j, j + k - 1);
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03216 (C++)

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

int n, q, m;
char s[N];

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (scanf("%d", &q); q; q--) {
    scanf("%d", &m);
    int l = 1, r = 0, sm = 0, sc = 0;
    long long res = 0, ans = 0;
    for (; r < m; r++) {
      if (s[r + 1] == 'C')
        res += sm, sc++;
      if (s[r + 1] == 'M')
        sm++;
    }
    for (; l <= n; l++, r++) {
      if (s[l] == 'D')
        ans += res;
      if (s[l] == 'M')
        res -= sc, sm--;
      if (s[l] == 'C')
        sc--;
      if (s[r + 1] == 'M')
        sm++;
      if (s[r + 1] == 'C')
        res += sm, sc++;
    }
    printf("%lld\n", ans);
  }
  return 0;
}

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

int n, q, m;
char s[N];

int main() {
  scanf("%d", &n);
  scanf("%s", s + 1);
  for (scanf("%d", &q); q; q--) {
    scanf("%d", &m);
    int l = 1, r = 0, sm = 0, sc = 0;
    long long res = 0, ans = 0;
    for (; r < m; r++) {
      if (s[r + 1] == 'C')
        res += sm, sc++;
      if (s[r + 1] == 'M')
        sm++;
    }
    for (; l <= n; l++, r++) {
      if (s[l] == 'D')
        ans += res;
      if (s[l] == 'M')
        res -= sc, sm--;
      if (s[l] == 'C')
        sc--;
      if (s[r + 1] == 'M')
        sm++;
      if (s[r + 1] == 'C')
        res += sm, sc++;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
Problem p03219 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int a, b;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> a >> b;
  cout << a + b / 2;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int a, b;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> a >> b;
  cout << a + b / 2;
}
Problem p03219 (C++)

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

using namespace std;

int main() {
  int x, y;
  int ans = 0;
  while (scanf("%d%d", &x, &y)) {
    ans = 0;
    ans = x + y / 2;
    printf("%d\n", ans);
  }
  return 0;
}

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

using namespace std;

int main() {
  int x, y;
  int ans = 0;
  scanf("%d%d", &x, &y);
  ans = 0;
  ans = x + y / 2;
  printf("%d\n", ans);
  return 0;
}
Problem p03219 (Python)

Buggy submission (status: Runtime Error):
x, y = map(int(), input().split(" "))
print(x + y // 2)


Fixed submission:
x, y = map(int, input().split(" "))
print(x + y // 2)
Problem p03219 (Python)

Buggy submission (status: Runtime Error):
x, y = map(int, input().split())

print(int(x + y / 2))
x, y = map(int, input().split())

print(int(x + y / 2))


Fixed submission:
x, y = map(int, input().split())

print(int(x + y / 2))
Problem p03219 (Python)

Buggy submission (status: Runtime Error):
def solve():
    x, y = map(int, input())
    return x + y // 2


if __name__ == "__main__":
    print(solve())


Fixed submission:
def solve():
    x, y = map(int, input().split())
    return x + y // 2


if __name__ == "__main__":
    print(solve())
Problem p03219 (C++)

Buggy submission (status: Runtime Error):
// Devarshi

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (i = 0; i < n; i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it, x) for (auto it : x)
#define PI 3.14159265
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 1000001;

int main() {

  sync

#ifndef ONLINE_JUDGE
      // for getting input from input.txt
      freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

  int x, y;
  cin >> x >> y;
  cout << x + y / 2;

  return 0;
}


Fixed submission:
// Devarshi

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (i = 0; i < n; i++)
#define forn(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (ll i = 1; i <= (ll)(n); ++i)
#define ford(i, n) for (ll i = (ll)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define fora(it, x) for (auto it : x)
#define PI 3.14159265
#define sync                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef long double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 1000001;

int main() {

  sync

      int x,
      y;
  cin >> x >> y;
  cout << x + y / 2;

  return 0;
}
Problem p03219 (C++)

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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  freopen("input.txt", "r", stdin);

  int x, y;
  cin >> x >> y;

  cout << x + y / 2 << "\n";
  return 0;
}


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

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int x, y;
  cin >> x >> y;

  cout << x + y / 2 << "\n";
  return 0;
}
Problem p03219 (C++)

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

#define LL long long

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.in", "r", stdin);
  freopen("output.out", "w", stdout);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int x, y;
  cin >> x >> y;

  cout << (x + y / 2) << endl;
}

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

#define LL long long

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int x, y;
  cin >> x >> y;

  cout << (x + y / 2) << endl;
}
Problem p03220 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
t, a = map(int, input().split())
list = [int(i) for i in input().split()]

dif_list = []
answer = 0

for i in range(n):
    t_i = t - list(i) * 0.006
    t_i_dif = abs(a - t_i)
    dif_list.append(t_i_dif)
    if min(dif_list) == t_i_dif:
        answer = i + 1

print(answer)


Fixed submission:
n = int(input())
t, a = map(int, input().split())
list = [int(i) for i in input().split()]

dif_list = []
answer = 0

for i in range(n):
    t_i = t - list[i] * 0.006
    t_i_dif = abs(a - t_i)
    dif_list.append(t_i_dif)
    if min(dif_list) == t_i_dif:
        answer = i + 1

print(answer)
Problem p03220 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
  int N, T, A;
  int H[N + 10], min = 100000, B[N + 10], ans;
  double tem;
  cin >> N >> T >> A;
  for (int i = 0; i < N; i++) {
    cin >> H[i];
  }

  for (int i = 0; i < N; i++) {
    tem = T - H[i] * 0.006;
    B[i] = A - tem;
    if (B[i] < 0) {
      B[i] = B[i] * (-1);
    }
  }

  for (int i = 0; i < N; i++) {
    if (min > B[i]) {
      min = B[i];
      ans = i;
    }
  }
  cout << ans + 1 << endl;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main() {
  int N, T, A;
  int H[1009], ans;
  double tem, B[1009], min = 10000000;
  cin >> N >> T >> A;
  for (int i = 0; i < N; i++) {
    cin >> H[i];
  }

  for (int i = 0; i < N; i++) {
    tem = T - H[i] * 0.006;
    B[i] = A - tem;
    if (B[i] < 0) {
      B[i] = B[i] * (-1);
    }
  }

  for (int i = 0; i < N; i++) {
    if (min > B[i]) {
      min = B[i];
      ans = i;
    }
  }
  cout << ans + 1 << endl;
}
Problem p03220 (C++)

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

double abs(double x, double y) {
  if (x >= y) {
    return x - y;
  } else {
    return y - x;
  }
}
int main() {
  int N, T, A;
  vector<int> H(N);
  for (int i = 0; i < N; i++) {
    cin >> H.at(i);
  }
  int ans = 1;
  double tem = abs(A, T - 0.006 * H.at(0));
  for (int i = 1; i < N; i++) {
    if (tem > abs(A, T - 0.006 * H.at(i))) {
      tem = abs(A, T - 0.006 * H.at(i));
      ans = i + 1;
    }
  }
  cout << ans << endl;
}

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

double abs(double x, double y) {
  if (x >= y) {
    return x - y;
  } else {
    return y - x;
  }
}
int main() {
  int N, T, A;
  cin >> N >> T >> A;
  vector<int> H(N);
  for (int i = 0; i < N; i++) {
    cin >> H.at(i);
  }
  int ans = 1;
  double tem = abs(A, T - 0.006 * H.at(0));
  for (int i = 1; i < N; i++) {
    if (tem > abs(A, T - 0.006 * H.at(i))) {
      tem = abs(A, T - 0.006 * H.at(i));
      ans = i + 1;
    }
  }
  cout << ans << endl;
}
Problem p03220 (C++)

Buggy submission (status: Runtime Error):
// Author: Jinhai Chen
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, A, t;
const int maxn = 1e5 + 10;
double h[maxn], a[maxn];

int main() {
#ifndef ONLINE_JUDGE
  freopen("data.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin >> n >> t >> A;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    a[i] = t - 0.006 * h[i];
  double g = 1e19;
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    if (fabs(a[i] - A) < g) {
      g = fabs(a[i] - A);
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// Author: Jinhai Chen
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, A, t;
const int maxn = 1e5 + 10;
double h[maxn], a[maxn];

int main() {
#ifndef ONLINE_JUDGE
//	freopen("data.in","r",stdin);
#endif
  ios::sync_with_stdio(false);
  cin >> n >> t >> A;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  for (int i = 1; i <= n; i++)
    a[i] = t - 0.006 * h[i];
  double g = 1e19;
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    if (fabs(a[i] - A) < g) {
      g = fabs(a[i] - A);
      ans = i;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03220 (C++)

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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  map<double, int> score;
  for (int i = 0; i < a; i++) {
    double d;
    cin >> d;
    score[b - d * 0.006] = i + 1;
  }
  double MIN = 10000000;
  int minkey = 0;
  for (auto p : score) {
    auto key = p.first;
    auto value = p.second;
    double lon = abs(key - c);
    if (lon < MIN) {
      MIN = lon;
      minkey = key;
    }
  }
  cout << score.at(minkey) << endl;
}


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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  map<double, int> score;
  for (int i = 0; i < a; i++) {
    double d;
    cin >> d;
    score[b - d * 0.006] = i + 1;
  }
  double MIN = 10000000;
  double minkey = 0;
  for (auto p : score) {
    auto key = p.first;
    auto value = p.second;
    double lon = abs(key - c);
    if (lon < MIN) {
      MIN = lon;
      minkey = key;
    }
  }
  cout << score.at(minkey) << endl;
}
Problem p03220 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int n, t, a;
  cin >> n >> t >> a;
  int h[100];
  rep(i, n) { cin >> h[i]; }

  float tmp, dif = FLT_MAX;
  int ans;
  rep(i, n) {
    tmp = t - h[i] * 0.006;
    if (dif > abs(tmp - a)) {
      ans = i + 1;
      dif = abs(tmp - a);
    }
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int main() {
  int n, t, a;
  cin >> n >> t >> a;
  int h[1000];
  rep(i, n) { cin >> h[i]; }

  float tmp, dif = FLT_MAX;
  int ans;
  rep(i, n) {
    tmp = t - h[i] * 0.006;
    if (dif > abs(tmp - a)) {
      ans = i + 1;
      dif = abs(tmp - a);
    }
  }
  cout << ans;

  return 0;
}
Problem p03220 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
t, a = map(int, input().split())
t *= 1000
a *= 1000
lst = list(map(int, input().split()))
tmp = abs(t - 6 * lst[0] - a)
tmpa = 0
for i in range(1, n):
    temp = abs(t - 6 * lst[i] - a)
    if temp < tmp:
        tmp = tmp
        tmpa = i
print(tmpa + 1)
n = int(input())
t, a = map(int, input().split())
t *= 1000
a *= 1000
lst = list(map(int, input().split()))
tmp = abs(t - 6 * lst[0] - a)
tmpa = 0
for i in range(1, n):
    temp = abs(t - 6 * lst[i] - a)
    if temp < tmp:
        tmp = temp
        tmpa = i
print(tmpa + 1)


Fixed submission:
n = int(input())
t, a = map(int, input().split())
t *= 1000
a *= 1000
lst = list(map(int, input().split()))
tmp = abs(t - 6 * lst[0] - a)
tmpa = 0
for i in range(1, n):
    temp = abs(t - 6 * lst[i] - a)
    if temp < tmp:
        tmp = temp
        tmpa = i
print(tmpa + 1)
Problem p03220 (C++)

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

#define foreach(i, v)                                                          \
  for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define BETWEEN(a, x, b) (a <= x && x < b)

using namespace std;
typedef long long ll;

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

  int n;
  double t, a;
  cin >> n >> t >> a;
  double h[100];
  int ok = 1;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    double v1 = t - h[ok] * 0.006;
    double v2 = t - h[i] * 0.006;
    if (abs(a - v1) >= abs(a - v2))
      ok = i;
  }
  cout << ok << endl;

  return 0;
}


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

#define foreach(i, v)                                                          \
  for (__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define BETWEEN(a, x, b) (a <= x && x < b)

using namespace std;
typedef long long ll;

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

  int n;
  double t, a;
  cin >> n >> t >> a;
  double h[1005];
  int ok = 1;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    double v1 = t - h[ok] * 0.006;
    double v2 = t - h[i] * 0.006;
    if (abs(a - v1) >= abs(a - v2))
      ok = i;
  }
  cout << ok << endl;

  return 0;
}
Problem p03220 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define M 1003
int main() {
  int N, T, A, H[N];
  double ret[N], re[N];
  cin >> N >> T >> A;
  for (int i = 0; i < N; i++) {
    cin >> H[i];
    ret[i] = abs(T - H[i] * 0.006 - A);
    re[i] = ret[i];
  }
  sort(re, re + N);
  for (int i = 0; i < N; i++) {
    if (ret[i] == re[0]) {
      cout << i + 1 << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define M 1003
int main() {
  int N, T, A, H[M];
  double ret[M], re[M];
  cin >> N >> T >> A;
  for (int i = 0; i < N; i++) {
    cin >> H[i];
    ret[i] = abs(T - H[i] * 0.006 - A);
    re[i] = ret[i];
  }
  sort(re, re + N);
  for (int i = 0; i < N; i++) {
    if (ret[i] == re[0]) {
      cout << i + 1 << endl;
      break;
    }
  }
  return 0;
}
Problem p03220 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
T, A = map(int, input().split())
(*H,) = [map(int, input().split())]
min = 10000000
ans = 0
for i in range(N):
    if min > abs(A - T + H[i] * 0.006):
        min = abs(A - T + H[i] * 0.006)
        ans = i
print(ans)


Fixed submission:
N = int(input())
T, A = map(int, input().split())
H = list(map(int, input().split()))
X = [abs(A - (T - h * 0.006)) for h in H]
print(X.index(min(X)) + 1)
Problem p03220 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
t, a = map(int, input().split())
b = [abs(a - (t - i * 0.006)) for i in list(map(int, input().split()))]
print(b.index(min(b) + 1))


Fixed submission:
n = int(input())
t, a = map(int, input().split())
b = [abs(a - (t - i * 0.006)) for i in list(map(int, input().split()))]
print(b.index(min(b)) + 1)
Problem p03220 (C++)

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

int main() {
  int N, T, A;

  cin >> N >> T >> A;
  vector<int> H(N);
  int res = 0;
  int cc = 1 << 30;
  for (int i = 1; i <= N; i++) {
    cin >> H.at(i);
    int d = (T * 1000 - H.at(i) * 6) - A * 1000;
    if (d < 0)
      d = -d;
    if (cc > d)
      cc = d, res = i;
  }
  cout << res << endl;
}

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

int main() {
  int N, T, A;

  cin >> N >> T >> A;
  vector<int> H(N + 10);
  int res = 0;
  int cc = 1 << 30;
  for (int i = 1; i <= N; i++) {
    cin >> H.at(i);
    int d = (T * 1000 - H.at(i) * 6) - A * 1000;
    if (d < 0)
      d = -d;
    if (cc > d)
      cc = d, res = i;
  }
  cout << res << endl;
}
Problem p03221 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define MOD (int)1e9 + 7
#define INF (int)2e9

int main() {
  int n, m;
  cin >> n >> m;
  int p[m], y[m];
  map<int, vector<int>> mp;

  rep(i, m) {
    cin >> p[i] >> y[i];
    mp[p[i]].emplace_back(y[i]);
  }

  for (auto &p : mp) {
    sort(p.second.begin(), p.second.end());
  }

  rep(i, m) {
    for (int j = 0; j < mp[p[i]].size(); j++) {
      if (mp[p[i]][j] == y[i]) {
        printf("%06d", p[i]);
        printf("%06d\n", j + 1);
        break;
      }
    }
  }
  return 0;
}

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

int main() {
  int n, m;
  cin >> n >> m;
  int p[m], y[m];
  map<int, vector<int>> mp;

  rep(i, m) {
    cin >> p[i] >> y[i];
    mp[p[i]].emplace_back(y[i]);
  }

  for (auto &p : mp) {
    sort(p.second.begin(), p.second.end());
  }

  rep(i, m) {
    int num =
        lower_bound(mp[p[i]].begin(), mp[p[i]].end(), y[i]) - mp[p[i]].begin();
    printf("%06d", p[i]);
    printf("%06d\n", num + 1);
  }
  return 0;
}
Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N, M, i;
  cin >> N;
  cin >> M;

  vector<pair<int, int>> p(M);
  vector<int> Y(M);
  vector<int> app(M);
  vector<string> SN(M);
  // map< int, int > orig;
  unordered_map<int, int> orig;

  int P;
  for (i = 0; i < M; i++) {
    cin >> P;
    cin >> Y[i];
    p[i] = make_pair(Y[i], P);
    orig[Y[i]] = i;
  }

  sort(p.begin(), p.end());

  int temp, num, j, index;
  for (i = 0; i < M; i++) {
    app[p[i].second]++;
    num = app[p[i].second];
    index = orig[p[i].first];
    string s1 = to_string(num);         // build order
    string s2 = to_string(p[i].second); // num of pref.
    for (j = 0; j < 6 - s2.size(); j++)
      SN[index].push_back('0');
    SN[index] += s2;
    for (j = 0; j < 6 - s1.size(); j++)
      SN[index].push_back('0');
    SN[index] += s1;
  }

  for (i = 0; i < M; i++) {
    cout << SN[i] << '\n';
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

int main() {
  int N, M, i;
  cin >> N;
  cin >> M;

  vector<pair<int, int>> p(M);
  vector<int> Y(M);
  vector<int> app(N);
  vector<string> SN(M);
  // map< int, int > orig;
  unordered_map<int, int> orig;

  int P;
  for (i = 0; i < M; i++) {
    cin >> P;
    cin >> Y[i];
    p[i] = make_pair(Y[i], P);
    orig[Y[i]] = i;
  }

  sort(p.begin(), p.end());

  int temp, num, j, index;
  for (i = 0; i < M; i++) {
    app[p[i].second]++;
    num = app[p[i].second];
    index = orig[p[i].first];
    string s1 = to_string(num);         // build order
    string s2 = to_string(p[i].second); // num of pref.
    for (j = 0; j < 6 - s2.size(); j++)
      SN[index].push_back('0');
    SN[index] += s2;
    for (j = 0; j < 6 - s1.size(); j++)
      SN[index].push_back('0');
    SN[index] += s1;
  }

  for (i = 0; i < M; i++) {
    cout << SN[i] << '\n';
  }
}
Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(c) c.begin(), c.end()

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> v(N + 1);
  REP(i, M) {
    int P, Y;
    cin >> P >> Y;
    v[P].PB(MP(Y, i));
  }

  vector<pair<int, int>> v2(N);
  for (int i = 1; i <= N; i++) {
    if (!v[i].empty()) {
      sort(ALL(v[i]));
      REP(j, v[i].size()) { v2[v[i][j].SE] = MP(i, j + 1); }
    }
  }
  REP(i, M) {
    cout << setfill('0') << setw(6) << v2[i].FI << setfill('0') << setw(6)
         << v2[i].SE << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(c) c.begin(), c.end()

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> v(N + 1);
  REP(i, M) {
    int P, Y;
    cin >> P >> Y;
    v[P].PB(MP(Y, i));
  }

  vector<pair<int, int>> v2(M);
  for (int i = 1; i <= N; i++) {
    if (!v[i].empty()) {
      sort(ALL(v[i]));
      REP(j, v[i].size()) { v2[v[i][j].SE] = MP(i, j + 1); }
    }
  }
  REP(i, M) {
    cout << setfill('0') << setw(6) << v2[i].FI << setfill('0') << setw(6)
         << v2[i].SE << endl;
  }
  return 0;
}
Problem p03221 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
struct cities {
  int pref, city, idx;
  bool operator<(const cities &c) const {
    return (pref < c.pref || city < c.city);
  }
};
int main() {
  int n, m, x = 0, y;
  cin >> n >> m;
  vector<cities> v(m);
  vector<pair<int, char *>> a(m);
  rep(i, m) {
    cin >> v[i].pref >> v[i].city;
    v[i].idx = i;
  }
  sort(v.begin(), v.end());
  rep(i, m) {
    if (x != v[i].pref) {
      x = v[i].pref;
      y = 1;
    }
    char *c = new char[13];
    sprintf(c, "%06d%06d", v[i].pref, y++);
    a[i].first = v[i].idx;
    a[i].second = c;
  }
  sort(a.begin(), a.end());
  rep(i, m) printf("%s\n", a[i].second);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
struct cities {
  int pref, city, idx;
  bool operator<(const cities &c) const {
    return (pref < c.pref || (pref == c.pref && city < c.city));
  }
};
int main() {
  int n, m, x = 0, y;
  cin >> n >> m;
  vector<cities> v(m);
  vector<pair<int, char *>> a(m);
  rep(i, m) {
    cin >> v[i].pref >> v[i].city;
    v[i].idx = i;
  }
  sort(v.begin(), v.end());
  rep(i, m) {
    if (x != v[i].pref) {
      x = v[i].pref;
      y = 1;
    }
    char *c = new char[13];
    sprintf(c, "%06d%06d", v[i].pref, y++);
    a[i].first = v[i].idx;
    a[i].second = c;
  }
  sort(a.begin(), a.end());
  rep(i, m) printf("%s\n", a[i].second);
  return 0;
}