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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> C(N);
  vector<ll> D(N);
  vector<ll> E(1000000);
  REPS(i, N) {
    C[i - 1] = A[i - 1] + i;
    D[i - 1] = i - A[i - 1];
  }
  REP(i, N) {
    if (D[i] >= 0) {
      E[D[i]]++;
    }
  }
  ll answer = 0;
  REP(i, N) {
    if (C[i] >= 0) {
      answer += E[C[i]];
    }
    if (D[i] >= 0) {
      E[D[i]]--;
    }
  }
  cout << answer << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

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

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> C(N);
  vector<ll> D(N);
  vector<ll> E(1000000);
  REPS(i, N) {
    C[i - 1] = A[i - 1] + i;
    D[i - 1] = i - A[i - 1];
  }
  REP(i, N) {
    if (D[i] >= 0) {
      E[D[i]]++;
    }
  }
  ll answer = 0;
  REP(i, N) {
    if (C[i] >= 0 && C[i] <= 800000) {
      answer += E[C[i]];
    }
    if (D[i] >= 0) {
      E[D[i]]--;
    }
  }
  cout << answer << "\n";
}
Problem p02691 (C++)

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

// j-i=a[j]+a[i]
// j-a[j]=i+a[i]
int main() {
  ll n;
  cin >> n;
  vector<ll> wa(200000), sa(20000);
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a + i + 1 < 200000)
      wa[a + i + 1]++;
    if (i + 1 - a >= 0)
      sa[i + 1 - a]++;
  }
  ll ans = 0;
  for (ll i = 1; i < 200000; i++) {
    ans += wa[i] * sa[i];
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1000000007;

// j-i=a[j]+a[i]
// j-a[j]=i+a[i]
int main() {
  ll n;
  cin >> n;
  vector<ll> wa(200000), sa(200000);
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    if (a + i + 1 < 200000)
      wa[a + i + 1]++;
    if (i + 1 - a >= 0)
      sa[i + 1 - a]++;
  }
  ll ans = 0;
  for (ll i = 1; i < 200000; i++) {
    ans += wa[i] * sa[i];
  }

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

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

int e() {
  int n;
  std::cin >> n;

  std::vector<long long> ha_a(n, 0);
  std::vector<long long> hb_b(n, 0);
  for (int i = 0; i < n; i++) {
    long long temp;
    std::cin >> temp;
    ha_a[i] = i + 1 - temp;
    hb_b[i] = i + 1 + temp;
  }

  std::sort(hb_b.begin(), hb_b.end());

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    std::vector<long long>::iterator it =
        std::lower_bound(hb_b.begin(), hb_b.end(), ha_a[i]);
    while (true) {
      if (*it != ha_a[i]) {
        break;
      }
      ans++;
      ++it;
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

int main(int argc, const char *argv[]) {
  e();
  return 0;
}

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

int e() {
  int n;
  std::cin >> n;

  std::vector<long long> ha_a(n, 0);
  std::vector<long long> hb_b(n, 0);
  for (int i = 0; i < n; i++) {
    long long temp;
    std::cin >> temp;
    ha_a[i] = i + 1 - temp;
    hb_b[i] = i + 1 + temp;
  }

  std::sort(hb_b.begin(), hb_b.end());

  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    std::vector<long long>::iterator it =
        std::lower_bound(hb_b.begin(), hb_b.end(), ha_a[i]);
    std::vector<long long>::iterator it2 =
        std::upper_bound(hb_b.begin(), hb_b.end(), ha_a[i]);
    if (*it == ha_a[i]) {
      ans += std::distance(it, it2);
    }
  }

  std::cout << ans << std::endl;

  return 0;
}

int main(int argc, const char *argv[]) {
  e();
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize ("O3")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

  int N;
  cin >> N;

  int A[100000];
  rep(i, N) cin >> A[i];

  map<int, int> MP;

  rep(i, N) { MP[i + A[i]]++; }

  ll kotae = 0;
  rep(i, N) {
    int tmp = i - A[i];
    kotae += MP[tmp];
  }

  co(kotae);

  Would you please return 0;
}

Fixed submission:
// #pragma GCC optimize ("O3")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

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

  int N;
  cin >> N;

  int A[200000];
  rep(i, N) cin >> A[i];

  map<int, int> MP;

  rep(i, N) { MP[i + A[i]]++; }

  ll kotae = 0;
  rep(i, N) {
    int tmp = i - A[i];
    kotae += MP[tmp];
  }

  co(kotae);

  Would you please return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar gc
inline char gc() {
  static char buf[1 << 16], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin);
    if (p2 == p1)
      return EOF;
  }
  return *p1++;
}
#endif
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 1e5 + 5;
long long ans;
int n, a[N], cnt[N];

signed main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = n; i; i--) {
    if (a[i] + i <= n)
      ans += cnt[a[i] + i];
    if (i >= a[i])
      cnt[i - a[i]]++;
  }
  write(ans);
}

Fixed submission:
#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar gc
inline char gc() {
  static char buf[1 << 16], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin);
    if (p2 == p1)
      return EOF;
  }
  return *p1++;
}
#endif
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 2e5 + 5;
long long ans;
int n, a[N], cnt[N];

signed main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = n; i; i--) {
    if (a[i] + i <= n)
      ans += cnt[a[i] + i];
    if (i >= a[i])
      cnt[i - a[i]]++;
  }
  write(ans);
}
Problem p02691 (C++)

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

int main() {
  long long int N;
  cin >> N;
  long long int A[N];
  for (long long int i = 0; i < N; i++)
    cin >> A[i];

  long long int help[N], help2[N];
  for (long long int i = 0; i < N; i++) {
    help[i] = i + 1 - A[i];
    help2[i] = i + 1 + A[i];
  }

  long long int aa[1000001], bb[1000001];

  for (long long int i = 0; i < 1000001; i++) {
    aa[i] = 0;
    bb[i] = 0;
  }

  for (long long int i = 0; i < N; i++) {
    aa[help[i] + 500001]++;
    bb[help2[i] + 500001]++;
  }

  long long int count = 0;

  for (long long int i = 0; i < 1000001; i++) {
    count += aa[i] * bb[i];
  }

  cout << count << endl;
}


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

int main() {
  long long int N;
  cin >> N;
  long long int A[N];
  for (long long int i = 0; i < N; i++)
    cin >> A[i];

  long long int help[N], help2[N];
  for (long long int i = 0; i < N; i++) {
    help[i] = i + 1 - A[i];
    help2[i] = i + 1 + A[i];
  }

  long long int aa[1000001], bb[1000001];

  for (long long int i = 0; i < 1000001; i++) {
    aa[i] = 0;
    bb[i] = 0;
  }

  for (long long int i = 0; i < N; i++) {
    if (help[i] >= 400000)
      continue;
    if (help2[i] >= 400000)
      continue;
    aa[help[i] + 500001]++;
    bb[help2[i] + 500001]++;
  }

  long long int count = 0;

  for (long long int i = 0; i < 1000001; i++) {
    count += aa[i] * bb[i];
  }

  cout << count << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
ll a[maxn], ans[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    ans[i + a[i]]++;
  }
  ll x = 0;
  for (int i = n; i >= 1; --i) {
    if (i - a[i] >= 200000 || i - a[i] <= 0)
      continue;
    x += ans[i - a[i]];
    if (i + a[i] < 200000)
      ans[i + a[i]]--;
  }
  cout << x << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
ll a[maxn], ans[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (i + a[i] < 200000)
      ans[i + a[i]]++;
  }
  ll x = 0;
  for (int i = n; i >= 1; --i) {
    if (i - a[i] >= 200000 || i - a[i] <= 0)
      continue;
    x += ans[i - a[i]];
    if (i + a[i] < 200000)
      ans[i + a[i]]--;
  }
  cout << x << endl;
  return 0;
}
Problem p02691 (C++)

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

#define int long long
int a[100010];
signed main() {
  int n;
  cin >> n;
  int ans = 0;
  map<int, int> mt;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    int t2 = i + a[i];
    if (t2 <= n && t2 > 0) {
      ++mt[t2];
    }
  }
  for (int i = 1; i <= n; ++i) {
    int t1 = i - a[i];
    if (t1 > 0) {
      ans += mt[t1];
    }
  }
  cout << ans << endl;
}


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

#define int long long
int a[200010];
signed main() {
  int n;
  cin >> n;
  int ans = 0;
  map<int, int> mt;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    int t2 = i + a[i];
    if (t2 <= n && t2 > 0) {
      ++mt[t2];
    }
  }
  for (int i = 1; i <= n; ++i) {
    int t1 = i - a[i];
    if (t1 > 0) {
      ans += mt[t1];
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define lowbit(i) (i & (-i))
typedef long long LL;
using namespace std;
const int maxn = 1e5 + 5;
int readint() {
  int x = 0, f = 1;
  char s = getchar();
#define sc (s = getchar())
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    sc;
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    sc;
  }
#undef sc
  return x * f;
}
int a[maxn];
map<int, int> mp, mp1;
signed main() {
  int n = readint();
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = readint();
  }
  for (int i = n; i >= 1; i--) {
    mp[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    mp1[i + a[i]]++;
    if (mp[i + a[i]] > 0)
      ans += mp[i + a[i]];
  }
  for (int i = n; i >= 1; i--)
    if (mp1[i - a[i]] > 0)
      ans += mp1[i - a[i]];
  cout << ans / 2;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define lowbit(i) (i & (-i))
typedef long long LL;
using namespace std;
const int maxn = 2e5 + 5;
int readint() {
  int x = 0, f = 1;
  char s = getchar();
#define sc (s = getchar())
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    sc;
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    sc;
  }
#undef sc
  return x * f;
}
int a[maxn];
map<int, int> mp, mp1;
signed main() {
  int n = readint();
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = readint();
  }
  for (int i = n; i >= 1; i--) {
    mp[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    mp1[i + a[i]]++;
    if (mp[i + a[i]] > 0)
      ans += mp[i + a[i]];
  }
  for (int i = n; i >= 1; i--)
    if (mp1[i - a[i]] > 0)
      ans += mp1[i - a[i]];
  cout << ans / 2;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
using namespace std;
typedef long long int lli;
const int MaxN = 100005;
map<lli, lli> mapi;
lli arr[MaxN];
int main() {
  lli N, M, i, j, p, q, res = 0;
  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (i = 1; i <= N; i++) {
    p = i - arr[i];
    q = i + arr[i];
    res += mapi[p];
    mapi[q] += 1;
  }
  cout << res;
}

Fixed submission:
#include <iostream>
#include <map>
using namespace std;
typedef long long int lli;
const int MaxN = 200005;
map<lli, lli> mapi;
lli arr[MaxN];
int main() {
  lli N, M, i, j, p, q, res = 0;
  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (i = 1; i <= N; i++) {
    p = i - arr[i];
    q = i + arr[i];
    res += mapi[p];
    mapi[q] += 1;
  }
  cout << res;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double PI = 3.14159265358979323846;
ll dp;

int main() {
  ll n, ans = 0, z[402020] = {};
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    if (i >= a)
      ans += z[i - a];
    z[a + i]++;
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define MAX(a, b) a = a > b ? a : b
#define MIN(a, b) a = a < b ? a : b
#define REP(i, x, n) for (int i = x; i < n; i++)
#define REPR(i, x, n) for (int i = n - 1; i >= x; i--)
#define pb push_back
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLr(obj) (obj).rbegin(), (obj).rend()
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double PI = 3.14159265358979323846;
ll dp;

int main() {
  ll n, ans = 0, z[402020] = {};
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    if (i >= a)
      ans += z[i - a];
    if (i + a < 401010)
      z[a + i]++;
    // cout<<ans<<endl;
  }
  cout << ans << endl;
}
Problem p02691 (C++)

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

using namespace std;

int main() {
  uint64_t N;
  uint64_t Ai[200000];
  uint64_t difAN[200000];

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

  for (uint64_t i = 0; i < N; i++) {
    uint64_t temp;
    cin >> temp;
    Ai[i] = temp + i + 1;
    if (i + 1 > temp) {
      difAN[i + 1 - temp]++;
    }
  }

  uint64_t countnum = 0;
  for (int i = 0; i < N; i++) {
    countnum += difAN[Ai[i]];
  }
  cout << countnum << endl;
}


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

using namespace std;

int main() {
  uint64_t N;
  uint64_t Ai[200000];
  uint64_t difAN[200000];

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

  for (uint64_t i = 0; i < N; i++) {
    uint64_t temp;
    cin >> temp;
    Ai[i] = temp + i + 1;
    if (i + 1 > temp) {
      difAN[i + 1 - temp]++;
    }
  }

  uint64_t countnum = 0;
  for (int i = 0; i < N; i++) {
    if (Ai[i] < N) {
      countnum += difAN[Ai[i]];
    }
  }
  cout << countnum << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
const int maxn = 1e5 + 5;
ll a[maxn];
map<ll, ll> mp;

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

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
const int maxn = 2e5 + 5;
ll a[maxn];
map<ll, ll> mp;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[-(a[i] + i)]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += mp[a[i] - i];
  }
  cout << ans << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
/*
          , -- 、
        /;;;;;;', 　　　
        i二二二i
      , ' 　    ヽ ???
     i　ﾉﾉﾉﾉﾉﾍ .', 　　
     〉ﾘi' ﾟ ヮﾟﾉﾚく　 WAyon..........
  ○⊂}〈lﾄ--ｲl〉{つ● 　　
      ,ｒ/ー i-ヽヽ 　　　
     ｀｀i_ﾗi_ﾗ
*/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int INF = (int)1e12;
const int mod = (int)1e9 + 7;
#define all(a) (a).begin(), (a).end()
typedef vector<vector<int>> graph;

#define pb push_back

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

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

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 100);
  rep(i, n) cin >> a[i];

  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    ans += cnt[a[i] + i];
    if (i - a[i] >= 0)
      cnt[i - a[i]]++;
  }
  cout << ans << endl;
}


Fixed submission:
/*
          , -- 、
        /;;;;;;', 　　　
        i二二二i
      , ' 　    ヽ ???
     i　ﾉﾉﾉﾉﾉﾍ .', 　　
     〉ﾘi' ﾟ ヮﾟﾉﾚく　 WAyon..........
  ○⊂}〈lﾄ--ｲl〉{つ● 　　
      ,ｒ/ー i-ヽヽ 　　　
     ｀｀i_ﾗi_ﾗ
*/

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define int long long
const int INF = (int)1e12;
const int mod = (int)1e9 + 7;
#define all(a) (a).begin(), (a).end()
typedef vector<vector<int>> graph;

#define pb push_back

struct UnionFind {
  vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) { // 最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++)
      par[i] = i;
  }

  int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) { // xとyの木を併合
    int rx = root(x);        // xの根をrx
    int ry = root(y);        // yの根をry
    if (rx == ry)
      return; // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry; // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

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

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> cnt(n + 100);
  rep(i, n) cin >> a[i];

  int ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] + i < n + 100)
      ans += cnt[a[i] + i];
    if (i - a[i] >= 0)
      cnt[i - a[i]]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 10, M = 1010;

LL a[N];
map<LL, LL> cnt;

void solve() {
  int n;
  cin >> n;
  LL res = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    res += cnt[i - a[i]];
    cnt[a[i] + i]++;
  }
  cout << res << endl;
}

int main() {
  IOS;
  int t;
  solve();
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  std::ios::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define FRE freopen("in.txt", "r", stdin);
#define endl "\n"
#define PI acos(-1.0)
typedef long long LL;
typedef pair<int, int> PII;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10, M = 1010;

LL a[N];
map<LL, LL> cnt;

void solve() {
  int n;
  cin >> n;
  LL res = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    res += cnt[i - a[i]];
    cnt[a[i] + i]++;
  }
  cout << res << endl;
}

int main() {
  IOS;
  int t;
  solve();
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(p, a, b) for (int p = a; p < b; p++)
#define INF 0x3f3f3f3f
#define N 100007

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

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

  ll n, i, ans = 0;
  map<ll, ll> m;

  cin >> n;
  vector<ll> h(n);
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    m[i + h[i]]++;
  }

  for (i = 1; i <= n; i++)
    ans += m[i - h[i]];
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(p, a, b) for (int p = a; p < b; p++)
#define INF 0x3f3f3f3f
#define N 100007

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

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

  ll n, i, ans = 0;
  map<ll, ll> m;

  cin >> n;
  vector<ll> h(n + 1);
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    m[i + h[i]]++;
  }

  for (i = 1; i <= n; i++)
    ans += m[i - h[i]];
  cout << ans;
  return 0;
}
Problem p02691 (C++)

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

using namespace std;
#define ll long long
const int MAX = 1e5 + 111;
int n;
ll h[MAX];
ll ans = 0;
map<ll, ll> p;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    ll m = i - h[i];
    if (p.count(m) != 0)
      ans += p[m];
    p[i + h[i]]++;
  }
  printf("%lld\n", ans);
}


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

using namespace std;
#define ll long long
const int MAX = 2e5 + 111;
int n;
ll h[MAX];
ll ans = 0;
map<ll, ll> p;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    ll m = i - h[i];
    if (p.count(m) != 0)
      ans += p[m];
    p[i + h[i]]++;
  }
  printf("%lld\n", ans);
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define repl(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
const int INF = 2147483647; // int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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() {
  ll n;
  cin >> n;
  vector<ll> h(n);
  rep(i, n) cin >> h[i];
  ll ans = 0;
  rep(i, n) {
    repl(j, i + 1, n) {
      if (abs(i - j) == h[i] + h[j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define repl(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
const int INF = 2147483647; // int max
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<int, int>;
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() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  rep(i, n) cin >> a[i];
  unordered_map<ll, ll> um;
  rep(i, n) um[i + a[i]]++;
  ll res = 0;
  rep(i, n) res += um[i - a[i]];
  cout << res << endl;
  return 0;
}
Problem p02691 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  long long N;
  cin >> N;

  vector<long long> a(N);
  vector<long long> score(N);
  for (int i = 0; i < N; ++i) {
    long long tmp;
    cin >> tmp;
    a[i] = tmp;
    score[i] = i - tmp;
  }

  vector<long long> s = score;
  sort(s.begin(), s.end());

  long long ret = 0;
  for (int i = 0; i < N; ++i) {
    long long cur = i + a[i];
    vector<long long>::iterator j = lower_bound(s.begin(), s.end(), i + a[i]);
    while (true) {
      if (j == s.end() || *j > cur)
        break;
      if (cur == *j)
        ++ret;
      ++j;
    }
  }

  cout << ret << endl;

  return 0;
}

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

using namespace std;

int main(int argc, char *argv[]) {
  long long N;
  cin >> N;

  vector<long long> a(N);
  vector<long long> score(N);
  for (int i = 0; i < N; ++i) {
    long long tmp;
    cin >> tmp;
    a[i] = tmp;
    score[i] = i - tmp;
  }

  vector<long long> s = score;
  sort(s.begin(), s.end());

  long long ret = 0;
  for (int i = 0; i < N; ++i) {
    long long cur = i + a[i];
    vector<long long>::iterator j = lower_bound(s.begin(), s.end(), i + a[i]);
    vector<long long>::iterator k = upper_bound(s.begin(), s.end(), i + a[i]);
    if (j == s.end() || *j > cur)
      continue;
    if (k == s.end())
      ret += distance(j, s.end());
    else
      ret += distance(j, k);
  }

  cout << ret << endl;

  return 0;
}
Problem p02692 (C++)

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

vector<ll> v;
vector<char> ans;
bool ok = 1;
ll n, t;
ll ind = 0;

void solve(string s) {
  if (!v[s[0] - 'A'] && !v[s[1] - 'A']) {
    ok = 0;
    return;
  }
  if (!v[s[0] - 'A'])
    v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
  else if (!v[s[1] - 'A'])
    v[s[1] - 'A']++, v[s[0] - 'A']--, ans[ind++] = s[1];
  else {
    if (t == 2) {
      if (n--) {
        string c;
        cin >> c;
        if (s[0] == c[0] || s[0] == c[1])
          v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
        else
          v[s[1] - 'A']++, v[s[0] - 'A']--, ans[ind++] = s[1];
        solve(c);
      } else
        v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
    } else {
      if (v[s[1] - 'A'] == t - 1)
        v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
      else
        v[s[1] - 'A']++, v[s[0] - 'A']--, ans[ind++] = s[1];
    }
  }
}

int main() {
  cin >> n;
  v.resize(3), ans.resize(n);
  cin >> v[0] >> v[1] >> v[2];
  t = v[0] + v[1] + v[2];
  while (n--) {
    string s;
    cin >> s;
    if (!ok)
      continue;
    solve(s);
  }
  if (n == 0 || !ok)
    cout << "No";
  else {
    cout << "Yes\n";
    for (char c : ans)
      cout << c << "\n";
  }
  return 0;
}


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

vector<ll> v;
vector<char> ans;
bool ok = 1;
ll n, t;
ll ind = 0;

void solve(string s) {
  if (!v[s[0] - 'A'] && !v[s[1] - 'A']) {
    ok = 0;
    return;
  }
  if (!v[s[0] - 'A'])
    v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
  else if (!v[s[1] - 'A'])
    v[s[1] - 'A']++, v[s[0] - 'A']--, ans[ind++] = s[1];
  else {
    if (t == 2) {
      if (n--) {
        string c;
        cin >> c;
        if (s[0] == c[0] || s[0] == c[1])
          v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
        else
          v[s[1] - 'A']++, v[s[0] - 'A']--, ans[ind++] = s[1];
        solve(c);
      } else
        v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
    } else {
      if (v[s[1] - 'A'] == t - 1)
        v[s[0] - 'A']++, v[s[1] - 'A']--, ans[ind++] = s[0];
      else
        v[s[1] - 'A']++, v[s[0] - 'A']--, ans[ind++] = s[1];
    }
  }
}

int main() {
  cin >> n;
  v.resize(3), ans.resize(n);
  cin >> v[0] >> v[1] >> v[2];
  t = v[0] + v[1] + v[2];
  while (n-- > 0) {
    string s;
    cin >> s;
    if (!ok)
      continue;
    solve(s);
  }
  if (n == 0 || !ok)
    cout << "No";
  else {
    cout << "Yes\n";
    for (char c : ans)
      cout << c << "\n";
  }
  return 0;
}
Problem p02692 (C++)

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

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (int i = 0; i < (int)(N); i++)

void solve() {
  int N;
  vector<int> D(3);
  cin >> N >> D;
  vector<string> s(N);
  cin >> s;

  vector<char> ans;
  rep(i, N) {
    int first = s[i][0] - 'A';
    int second = s[i][1] - 'A';
    if (D[first] == 0 and D[second] == 0) {
      cout << "No" << endl;
      return;
    }

    int greater;
    int less;

    if (D[first] == 1 and D[second] == 1 and i + 1 < N) {
      int rest = 0 + 1 + 2 - first - second;
      if (count(all(s[i + 1]), (char)('A' + rest)) == 1) {
        if (count(all(s[i + 1]), (char)('A' + first)) == 1) {
          greater = second;
          less = first;
        } else {
          greater = first;
          less = second;
        }
      }
    } else if (D[first] >= D[second]) {
      greater = first;
      less = second;
    } else {
      greater = second;
      less = first;
    }

    D[greater]--;
    D[less]++;
    ans.push_back('A' + less);
  }
  cout << "Yes" << endl;
  rep(i, N) cout << ans[i] << endl;
}

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


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

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
#define all(x) (x).begin(), (x).end()
#define rep(i, N) for (int i = 0; i < (int)(N); i++)

void solve() {
  int N;
  vector<int> D(3);
  cin >> N >> D;
  vector<string> s(N);
  cin >> s;

  vector<char> ans;
  rep(i, N) {
    int first = s[i][0] - 'A';
    int second = s[i][1] - 'A';
    if (D[first] == 0 and D[second] == 0) {
      cout << "No" << endl;
      return;
    }

    int greater;
    int less;

    int rest = 0 + 1 + 2 - first - second;
    if (D[first] == 1 and D[second] == 1 and i + 1 < N and
        count(all(s[i + 1]), (char)('A' + rest)) == 1) {
      if (count(all(s[i + 1]), (char)('A' + first)) == 1) {
        greater = second;
        less = first;
      } else {
        greater = first;
        less = second;
      }
    } else if (D[first] >= D[second]) {
      greater = first;
      less = second;
    } else {
      greater = second;
      less = first;
    }

    D[greater]--;
    D[less]++;
    ans.push_back('A' + less);
  }
  cout << "Yes" << endl;
  rep(i, N) cout << ans[i] << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02692 (C++)

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

#define x first
#define y second
#define pb push_back
#define mp make_pair

#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()

#define ll long long
#define ld long double

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  map<char, int> ma;
  cin >> n >> ma['A'] >> ma['B'] >> ma['C'];
  vector<string> q(n);
  for (int i = 0; i < n; ++i)
    cin >> q[i];
  vector<map<char, int>> pr(n + 1);
  pr[0] = ma;
  string ans = "";
  for (int i = 0; i < n; ++i) {
    string s = q[i];
    if (ma[s[0]] == 0 && ma[s[1]] == 0) {
      if (i == 0) {
        cout << "No\n";
        return 0;
      }
      assert(q[i - 1][1] != s[0] && q[i - 1][1] != s[1]);
      if (pr[i - 1][q[i - 1][1]] == 0) {
        cout << "No\n";
        return 0;
      }
      ans.back() = q[i - 1][0];
      pr[i][q[i - 1][1]] -= 2;
      pr[i][q[i - 1][0]] += 2;
      ma = pr[i];
      char ch = 'A';
      while (ch == q[i - 1][0] || ch == q[i - 1][1])
        ++ch;
      ans.pb(ch);
      ++ma[ch];
      --ma[q[i - 1][0]];
    } else {
      if (ma[s[0]] == 0) {
        ans.pb(s[0]);
        ++ma[s[0]];
        --ma[s[1]];
      } else {
        ans.pb(s[1]);
        ++ma[s[1]];
        --ma[s[0]];
      }
    }
    pr[i + 1] = ma;
  }
  cout << "Yes\n";
  for (auto x : ans)
    cout << x << '\n';
  return 0;
}


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

#define x first
#define y second
#define pb push_back
#define mp make_pair

#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()

#define ll long long
#define ld long double

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  map<char, int> ma;
  cin >> n >> ma['A'] >> ma['B'] >> ma['C'];
  vector<string> q(n);
  for (int i = 0; i < n; ++i)
    cin >> q[i];
  vector<map<char, int>> pr(n + 1);
  pr[0] = ma;
  string ans = "";
  for (int i = 0; i < n; ++i) {
    string s = q[i];
    if (ma[s[0]] == 0 && ma[s[1]] == 0) {
      if (i == 0) {
        cout << "No\n";
        return 0;
      }
      if (q[i - 1][1] == s[0] || q[i - 1][1] == s[1])
        swap(q[i - 1][1], q[i - 1][0]);
      if (pr[i - 1][q[i - 1][1]] == 0) {
        cout << "No\n";
        return 0;
      }
      ans.back() = q[i - 1][0];
      pr[i][q[i - 1][1]] -= 2;
      pr[i][q[i - 1][0]] += 2;
      ma = pr[i];
      char ch = 'A';
      while (ch == q[i - 1][0] || ch == q[i - 1][1])
        ++ch;
      ans.pb(ch);
      ++ma[ch];
      --ma[q[i - 1][0]];
    } else {
      if (ma[s[0]] == 0) {
        ans.pb(s[0]);
        ++ma[s[0]];
        --ma[s[1]];
      } else {
        ans.pb(s[1]);
        ++ma[s[1]];
        --ma[s[0]];
      }
    }
    pr[i + 1] = ma;
  }
  cout << "Yes\n";
  for (auto x : ans)
    cout << x << '\n';
  return 0;
}
Problem p02692 (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;

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  vector<char> ans(n);
  rep(i, n) cin >> s.at(i);

  bool ok = true;

  auto next = [&](int i, int &x, int &y) {
    if (x + y == 0) {
      ok = false;
      return ok;
    }
    if (x == 0) {
      ans.at(i) = s.at(i).at(0);
      x++;
      y--;
    } else if (y == 0) {
      ans.at(i) = s.at(i).at(1);
      x--;
      y++;
    } else {
      if (s.at(i + 1).find(s.at(i).at(0)) != string::npos && i < n - 1) {
        ans.at(i) = s.at(i).at(0);
        x++;
        y--;
      } else {
        ans.at(i) = s.at(i).at(1);
        x--;
        y++;
      }
    }
    return true;
  };

  rep(i, n) {
    if (s.at(i) == "AB") {
      if (!next(i, a, b))
        break;
    } else if (s.at(i) == "AC") {
      if (!next(i, a, c))
        break;
    } else if (s.at(i) == "BC") {
      if (!next(i, b, c))
        break;
    }
  }

  if (ok) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans.at(i) << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

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

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  vector<char> ans(n);
  rep(i, n) cin >> s.at(i);

  bool ok = true;

  auto next = [&](int i, int &x, int &y) {
    if (x + y == 0) {
      ok = false;
      return ok;
    }
    if (x == 0) {
      ans.at(i) = s.at(i).at(0);
      x++;
      y--;
    } else if (y == 0) {
      ans.at(i) = s.at(i).at(1);
      x--;
      y++;
    } else {
      if (i < n - 1 && s.at(i + 1).find(s.at(i).at(0)) != string::npos) {
        ans.at(i) = s.at(i).at(0);
        x++;
        y--;
      } else {
        ans.at(i) = s.at(i).at(1);
        x--;
        y++;
      }
    }
    return true;
  };

  rep(i, n) {
    if (s.at(i) == "AB") {
      if (!next(i, a, b))
        break;
    } else if (s.at(i) == "AC") {
      if (!next(i, a, c))
        break;
    } else if (s.at(i) == "BC") {
      if (!next(i, b, c))
        break;
    }
  }

  if (ok) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans.at(i) << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p02692 (C++)

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

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (auto &e : S)
    cin >> e;

  string ans = "", tmp = "";
  auto dfs = [&](auto &&dfs, int i, int a, int b, int c) -> bool {
    if (a < 0 or b < 0 or c < 0)
      return false;
    if (i == N) {
      ans = tmp;
      return true;
    }
    bool res = false;
    if (S[i] == "AB") {
      tmp += 'A';
      res |= dfs(dfs, i + 1, a + 1, b - 1, c);
      tmp[i] = 'B';
      res |= dfs(dfs, i + 1, a - 1, b + 1, c);
      tmp.pop_back();
    } else if (S[i] == "BC") {
      tmp += 'B';
      res |= dfs(dfs, i + 1, a, b + 1, c - 1);
      tmp[i] = 'C';
      res |= dfs(dfs, i + 1, a, b - 1, c + 1);
      tmp.pop_back();
    } else if (S[i] == "AC") {
      tmp += 'A';
      res |= dfs(dfs, i + 1, a + 1, b, c - 1);
      tmp[i] = 'C';
      res |= dfs(dfs, i + 1, a - 1, b, c + 1);
      tmp.pop_back();
    }
    return res;
  };

  if (dfs(dfs, 0, A, B, C)) {
    cout << "Yes" << '\n';
    for (auto &e : ans)
      cout << e << '\n';
  } else {
    cout << "No" << '\n';
  }
  return 0;
}


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

int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S(N);
  for (auto &e : S)
    cin >> e;

  string ans = "", tmp = "";
  auto dfs = [&](auto &&dfs, int i, int a, int b, int c) -> bool {
    if (!ans.empty())
      return true;
    if (a < 0 or b < 0 or c < 0)
      return false;
    if (i == N) {
      ans = tmp;
      return true;
    }
    bool res = false;
    if (S[i] == "AB") {
      tmp += 'A';
      res |= dfs(dfs, i + 1, a + 1, b - 1, c);
      tmp[i] = 'B';
      res |= dfs(dfs, i + 1, a - 1, b + 1, c);
      tmp.pop_back();
    } else if (S[i] == "BC") {
      tmp += 'B';
      res |= dfs(dfs, i + 1, a, b + 1, c - 1);
      tmp[i] = 'C';
      res |= dfs(dfs, i + 1, a, b - 1, c + 1);
      tmp.pop_back();
    } else if (S[i] == "AC") {
      tmp += 'A';
      res |= dfs(dfs, i + 1, a + 1, b, c - 1);
      tmp[i] = 'C';
      res |= dfs(dfs, i + 1, a - 1, b, c + 1);
      tmp.pop_back();
    }
    return res;
  };

  if (dfs(dfs, 0, A, B, C)) {
    cout << "Yes" << '\n';
    for (auto &e : ans)
      cout << e << '\n';
  } else {
    cout << "No" << '\n';
  }
  return 0;
}
Problem p02692 (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++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

void app() {
  int n, a[3];
  cin >> n >> a[0] >> a[1] >> a[2];

  vector<string> sv(n);
  rep(i, n) { cin >> sv[i]; }

  string ans;
  rep(i, n) {
    string s = sv[i];
    int c = s[0] - 'A';
    int d = s[1] - 'A';

    if (a[c] < a[d]) {
      ans += 'A' + c;
      a[c]++;
      a[d]--;
    } else if (a[c] > a[d]) {
      ans += 'A' + d;
      a[d]++;
      a[c]--;
    } else if (i == n - 1 || s[i] == s[i + 1]) {
      ans += 'A' + c;
      a[c]++;
      a[d]--;
    } else {
      string sn = sv[i + 1];
      int e = sn[0] - 'A';
      int f = sn[1] - 'A';
      if (c == e || c == f) {
        ans += 'A' + c;
        a[c]++;
        a[d]--;
      } else {
        ans += 'A' + d;
        a[d]++;
        a[c]--;
      }
    }

    if (a[0] < 0 || a[1] < 0 || a[2] < 0) {
      cout << "No" << endl;
      return;
    }
  }

  cout << "Yes" << endl;
  rep(i, n) { cout << ans[i] << endl; }
}

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


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

void app() {
  int n, a[3];
  cin >> n >> a[0] >> a[1] >> a[2];

  vector<string> sv(n);
  rep(i, n) { cin >> sv[i]; }

  string ans;
  rep(i, n) {
    string s = sv[i];
    int c = s[0] - 'A';
    int d = s[1] - 'A';

    if (a[c] < a[d]) {
      ans += 'A' + c;
      a[c]++;
      a[d]--;
    } else if (a[c] > a[d]) {
      ans += 'A' + d;
      a[d]++;
      a[c]--;
    } else if (i == n - 1 || sv[i] == sv[i + 1]) {
      ans += 'A' + c;
      a[c]++;
      a[d]--;
    } else {
      string sn = sv[i + 1];
      int e = sn[0] - 'A';
      int f = sn[1] - 'A';
      if (c == e || c == f) {
        ans += 'A' + c;
        a[c]++;
        a[d]--;
      } else {
        ans += 'A' + d;
        a[d]++;
        a[c]--;
      }
    }

    if (a[0] < 0 || a[1] < 0 || a[2] < 0) {
      cout << "No" << endl;
      return;
    }
  }

  cout << "Yes" << endl;
  rep(i, n) { cout << ans[i] << endl; }
}

int main() {
  app();
  return 0;
}
Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

int main() {
  int N, A[3], i, j, k, nj, nk;
  char s[3];
  int ans[10000];
  scanf("%d%d%d%d", &N, A, A + 1, A + 2);
  // if(A[0] + A[1] + A[2] == 0){
  //   printf("No");
  // }
  scanf("%s", s);
  if (strcmp(s, "AB") == 0) {
    j = 0;
    k = 1;
  }
  if (strcmp(s, "AC") == 0) {
    j = 0;
    k = 2;
  }
  if (strcmp(s, "BC") == 0) {
    j = 1;
    k = 2;
  }
  for (i = 0; i < N; i++) {
    if (i != N - 1) {
      scanf("%s", s);
      if (strcmp(s, "AB") == 0) {
        nj = 0;
        nk = 1;
      }
      if (strcmp(s, "AC") == 0) {
        nj = 0;
        nk = 2;
      }
      if (strcmp(s, "BC") == 0) {
        nj = 1;
        nk = 2;
      }
    }
    if (A[j] == 0 && A[k] == 0) {
      printf("No");
      // break;
      return 0;
    }
    if (A[j] == 0) {
      A[j]++;
      A[k]--;
      ans[i] = j;
    } else if (A[k] == 0) {
      A[j]--;
      A[k]++;
      ans[i] = k;
    } else if (i != N - 1) {
      if (j == nj || j == nk) {
        A[j]++;
        A[k]--;
        ans[i] = j;
      } else {
        A[j]--;
        A[k]++;
        ans[i] = k;
      }
    } else {
      A[j]++;
      A[k]--;
      ans[i] = j;
    }
    j = nj;
    k = nk;
  }
  printf("Yes\n");
  for (i = 0; i < N; i++) {
    printf("%c\n", 'A' + ans[i]);
  }
}

Fixed submission:
#include <stdio.h>
#include <string.h>

int main() {
  int N, A[3], i, j, k, nj, nk;
  char s[3];
  int ans[100000];
  scanf("%d%d%d%d", &N, A, A + 1, A + 2);
  // if(A[0] + A[1] + A[2] == 0){
  //   printf("No");
  // }
  scanf("%s", s);
  if (strcmp(s, "AB") == 0) {
    j = 0;
    k = 1;
  }
  if (strcmp(s, "AC") == 0) {
    j = 0;
    k = 2;
  }
  if (strcmp(s, "BC") == 0) {
    j = 1;
    k = 2;
  }
  for (i = 0; i < N; i++) {
    if (i != N - 1) {
      scanf("%s", s);
      if (strcmp(s, "AB") == 0) {
        nj = 0;
        nk = 1;
      }
      if (strcmp(s, "AC") == 0) {
        nj = 0;
        nk = 2;
      }
      if (strcmp(s, "BC") == 0) {
        nj = 1;
        nk = 2;
      }
    }
    if (A[j] == 0 && A[k] == 0) {
      printf("No");
      // break;
      return 0;
    }
    if (A[j] == 0) {
      A[j]++;
      A[k]--;
      ans[i] = j;
    } else if (A[k] == 0) {
      A[j]--;
      A[k]++;
      ans[i] = k;
    } else if (i != N - 1) {
      if (j == nj || j == nk) {
        A[j]++;
        A[k]--;
        ans[i] = j;
      } else {
        A[j]--;
        A[k]++;
        ans[i] = k;
      }
    } else {
      A[j]++;
      A[k]--;
      ans[i] = j;
    }
    j = nj;
    k = nk;
  }
  printf("Yes\n");
  for (i = 0; i < N; i++) {
    printf("%c\n", 'A' + ans[i]);
  }
}