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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int Max = 1000000;
int main() {
  int N;
  cin >> N;
  int T[N], A[N];
  for (int i = 0; i < N; i++) {
    cin >> T[i] >> A[i];
  }
  ll ansT = T[0];
  ll ansA = A[0];

  for (int i = 1; i < N; i++) {
    ll tmp = 1;
    while (tmp * T[i] < ansT || tmp * A[i] < ansA) {
      tmp++;
    }
    ansT = tmp * T[i];
    ansA = tmp * A[i];
  }
  cout << ansT + ansA << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int Max = 1000000;
int main() {
  int N;
  cin >> N;
  int T[N], A[N];
  for (int i = 0; i < N; i++) {
    cin >> T[i] >> A[i];
  }
  ll ansT = T[0];
  ll ansA = A[0];

  for (int i = 1; i < N; i++) {
    ll tmp = max((ansT + T[i] - 1) / T[i], (ansA + A[i] - 1) / A[i]);
    ansT = tmp * T[i];
    ansA = tmp * A[i];
  }
  cout << ansT + ansA << endl;

  return 0;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
long long a[1111], t[1111];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long p, q;
    cin >> p >> q;
    a[i] = p;
    t[i] = q;
    while (a[i] < a[i - 1] || t[i] < t[i - 1]) {
      a[i] += p;
      t[i] += q;
    }
  }
  cout << a[n] + t[n];
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long a[1111], t[1111];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long p, q;
    cin >> p >> q;
    a[i] = p;
    t[i] = q;
    if (a[i] >= a[i - 1] && t[i] >= t[i - 1])
      continue;

    long long m =
        max((a[i] + a[i - 1] - 1) / a[i], (t[i] + t[i - 1] - 1) / t[i]);
    a[i] = a[i] * m;
    t[i] = t[i] * m;
    //	cout<<i<<" "<<m<<" "<<a[i]<<" "<<t[i];
    /*
    while(a[i]<a[i-1]||t[i]<t[i-1])
    {
            a[i]+=p;
            t[i]+=q;
    }*/
  }
  cout << a[n] + t[n];
  return 0;
}
Problem p03966 (C++)

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

using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

int main() {
  int N;
  cin >> N;
  LL na = -1, nb = -1;
  REP(i, N) {
    LL a, b;
    cin >> a >> b;
    if (na == nb && na == -1) {
      na = a;
      nb = b;
    } else {
      LL hi = 1;
      while (a * hi < na || b * hi < nb)
        hi++;
      na = a * hi;
      nb = b * hi;
    }
  }
  cout << na + nb << endl;
  return 0;
}


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

using namespace std;

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

int main() {
  int N;
  cin >> N;
  LL na = -1, nb = -1;
  REP(i, N) {
    LL a, b;
    cin >> a >> b;
    if (na == nb && na == -1) {
      na = a;
      nb = b;
    } else {
      LL hi = max((na / a + (na % a != 0)), (nb / b + (nb % b != 0)));
      na = a * hi;
      nb = b * hi;
    }
  }
  cout << na + nb << endl;
  return 0;
}
Problem p03966 (C++)

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

int main() {
  int n;
  cin >> n;
  long long int a[n];
  long long int t[n];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (t[i] > t[i + 1] || a[i] > a[i + 1]) {
      for (int j = max(t[i] / t[i + 1], a[i] / a[i + 1]);; j++) {
        if (t[i] <= t[i + 1] * j && a[i] <= a[i + 1] * j) {
          t[i + 1] = t[i + 1] * j;
          a[i + 1] = a[i + 1] * j;
          break;
        }
      }
    }
  }
  cout << t[n - 1] + a[n - 1] << endl;
}

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

int main() {
  int n;
  cin >> n;
  long long int a[n];
  long long int t[n];
  for (int i = 0; i < n; i++) {
    cin >> t[i] >> a[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (t[i] > t[i + 1] || a[i] > a[i + 1]) {
      for (long long int j = max(t[i] / t[i + 1], a[i] / a[i + 1]);; j++) {
        if (t[i] <= t[i + 1] * j && a[i] <= a[i + 1] * j) {
          t[i + 1] = t[i + 1] * j;
          a[i + 1] = a[i + 1] * j;
          break;
        }
      }
    }
  }
  cout << t[n - 1] + a[n - 1] << endl;
}
Problem p03966 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i >= n; --i)
typedef long long ll;
#define mod 1000000007
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;
}

int main() {
  int N;
  cin >> N;
  ll cl, cr;
  cin >> cl >> cr;
  rep(i, 1, N) {
    ll l, r;
    cin >> l >> r;
    if (cl <= l && cr <= r) {
      cl = l, cr = r;
    } else {
      ll nl = l, nr = r;
      while (nl < cl || nr < cr) {
        nl += l, nr += r;
      }
      cl = nl, cr = nr;
    }
  }
  cout << cl + cr << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i >= n; --i)
typedef long long ll;
#define mod 1000000007
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;
}

int main() {
  int N;
  cin >> N;
  ll cl, cr;
  cin >> cl >> cr;
  rep(i, 1, N) {
    ll l, r;
    cin >> l >> r;
    if (cl <= l && cr <= r) {
      cl = l, cr = r;
    } else {
      ll nl = cl / l, nr = cr / r;
      nl += (cl % l != 0), nr += (cr % r != 0);
      ll m = max(nl, nr);
      cl = l * m, cr = r * m;
    }
  }
  cout << cl + cr << endl;
  return 0;
}
Problem p03966 (C++)

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

int main() {
  int n;
  cin >> n;
  vector<ll> t(n), a(n);
  rep(i, n) cin >> t[i] >> a[i];
  rep(i, n - 1) {
    ll x = t[i + 1], y = a[i + 1];
    while (1) {
      if (t[i + 1] >= t[i] && a[i + 1] >= a[i])
        break;
      t[i + 1] += x, a[i + 1] += y;
    }
  }
  cout << t[n - 1] + a[n - 1] << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<ll> t(n), a(n);
  rep(i, n) cin >> t[i] >> a[i];
  rep(i, n - 1) {
    ll mx =
        max((t[i] + t[i + 1] - 1) / t[i + 1], (a[i] + a[i + 1] - 1) / a[i + 1]);
    t[i + 1] *= mx, a[i + 1] *= mx;
  }
  cout << t[n - 1] + a[n - 1] << endl;
}
Problem p03966 (Python)

Buggy submission (status: Time Limit Exceeded):
a = int(input())
d = e = 0
for i in range(a):
    b, c = map(int, input().split())
    if i == 0:
        d = b
        e = c
    else:
        f = b
        g = c
        while True:
            if d <= f and e <= g:
                d = f
                e = g
                break
            else:
                f += b
                g += c
print(d + e)


Fixed submission:
a = int(input())
d = e = 0
for i in range(a):
    b, c = map(int, input().split())
    if i == 0:
        d = b
        e = c
    else:
        if d <= b and e <= c:
            d = b
            e = c
        else:
            h = max((b + d - 1) // b, (c + e - 1) // c)
            d = b * h
            e = c * h
print(d + e)
Problem p03967 (C++)

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

int main(void) {
  char s[12000];
  scanf("%s", s);
  int n = strlen(s);
  int res;
  for (int i = 0; i < n; i++) {
    if (i % 2 == 0) {
      if (s[i] == 'p')
        res--;
    } else {
      if (s[i] == 'g')
        res++;
    }
  }
  printf("%d\n", res);
  return 0;
}

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

int main(void) {
  char s[120000];
  scanf("%s", s);
  int n = strlen(s);
  int res;
  for (int i = 0; i < n; i++) {
    if (i % 2 == 0) {
      if (s[i] == 'p')
        res--;
    } else {
      if (s[i] == 'g')
        res++;
    }
  }
  printf("%d\n", res);
  return 0;
}
Problem p03967 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
int n, x;
char s[10001];
int main() {
  scanf(" %s", s);
  while (s[n] != NULL)
    n++;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'p')
      x--;
  }
  printf("%d\n", x + n / 2);
}

Fixed submission:
#include <stdio.h>
int n, x;
char s[100001];
int main() {
  scanf(" %s", s);
  while (s[n] != NULL)
    n++;
  for (int i = 0; i < n; i++) {
    if (s[i] == 'p')
      x--;
  }
  printf("%d\n", x + n / 2);
}
Problem p03967 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll, ll> pii;
int main() {
  string S;
  cin >> S;
  ll N = S.length();
  vvi dp(N, vi(N + 1, -INF));
  dp[0][0] = 0;
  FOR(i, 1, N) REP(j, N + 1) if (2 * j <= i + 1) {
    cout << i << ' ' << j << endl;
    if (S[i] == 'g') {
      if (j)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      dp[i][j] = max(dp[i][j], dp[i - 1][j]);
    } else {
      if (j)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
      dp[i][j] = max(dp[i][j], dp[i - 1][j] - 1);
    }
  }
  ll ans = -INF;
  for (ll i : dp[N - 1])
    ans = max(ans, i);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define UM unordered_map
#define US unordered_set
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
const ll INF = (1ll << 60);
typedef pair<ll, ll> pii;
int main() {
  string S;
  cin >> S;
  ll cnt = 0;
  for (char c : S)
    if (c == 'p')
      cnt++;
  cout << S.length() / 2 - cnt << endl;
}
Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <map>
using namespace std;
const int MAXN = 405;

long long encode(long long a, long long b, long long c, long long d) {
  return (a << 30LL) | (b << 20LL) | (c << 10LL) | d;
}

map<long long, long long> cnt;
void Add(long long c, int delta) {
  for (int i = 0; i < 4; i++) {
    cnt[c] += delta;
    c = (c >> 10LL) | ((c & 1023LL) << 30LL);
  }
}

int n;
int c[MAXN][4];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d", c[i], c[i] + 1, c[i] + 2, c[i] + 3);
    Add(encode(c[i][0], c[i][1], c[i][2], c[i][3]), 1);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    Add(encode(c[i][0], c[i][1], c[i][2], c[i][3]), -1);
    for (int j = i + 1; j <= n; j++) {
      Add(encode(c[j][0], c[j][1], c[j][2], c[j][3]), -1);
      for (int k = 0; k < 4; k++) {
        long long u[4] = {
            encode(c[i][1], c[i][0], c[j][(k + 1) & 3], c[j][(k + 0) & 3]),
            encode(c[i][0], c[i][3], c[j][(k + 2) & 3], c[j][(k + 1) & 3]),
            encode(c[i][3], c[i][2], c[j][(k + 3) & 3], c[j][(k + 2) & 3]),
            encode(c[i][2], c[i][1], c[j][(k + 0) & 3], c[j][(k + 3) & 3])};
        long long tmp = 1;
        for (int l = 0; l < 4; l++)
          tmp *= cnt[u[l]], Add(u[l], -1);
        for (int l = 0; l < 4; l++)
          Add(u[l], 1);
        ans += tmp;
      }
      Add(encode(c[j][0], c[j][1], c[j][2], c[j][3]), 1);
    }
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <cstdio>
#include <map>
using namespace std;
const int MAXN = 405;

long long encode(long long a, long long b, long long c, long long d) {
  return (a << 30LL) | (b << 20LL) | (c << 10LL) | d;
}

map<long long, long long> cnt;
void Add(long long c, int delta) {
  for (int i = 0; i < 4; i++) {
    cnt[c] += delta;
    c = (c >> 10LL) | ((c & 1023LL) << 30LL);
  }
}

int n;
int c[MAXN][4];

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d%d%d", c[i], c[i] + 1, c[i] + 2, c[i] + 3);
    Add(encode(c[i][0], c[i][1], c[i][2], c[i][3]), 1);
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    Add(encode(c[i][0], c[i][1], c[i][2], c[i][3]), -1);
    for (int j = i + 1; j <= n; j++) {
      Add(encode(c[j][0], c[j][1], c[j][2], c[j][3]), -1);
      for (int k = 0; k < 4; k++) {
        long long u[4] = {
            encode(c[i][1], c[i][0], c[j][(k + 1) & 3], c[j][(k + 0) & 3]),
            encode(c[i][0], c[i][3], c[j][(k + 2) & 3], c[j][(k + 1) & 3]),
            encode(c[i][3], c[i][2], c[j][(k + 3) & 3], c[j][(k + 2) & 3]),
            encode(c[i][2], c[i][1], c[j][(k + 0) & 3], c[j][(k + 3) & 3])};
        if (cnt[u[0]] && cnt[u[1]] && cnt[u[2]] && cnt[u[3]]) {
          long long tmp = 1;
          for (int l = 0; l < 4; l++)
            tmp *= cnt[u[l]], Add(u[l], -1);
          for (int l = 0; l < 4; l++)
            Add(u[l], 1);
          ans += tmp;
        }
      }
      Add(encode(c[j][0], c[j][1], c[j][2], c[j][3]), 1);
    }
  }
  printf("%lld\n", ans);

  return 0;
}
Problem p03968 (C++)

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

#define ll long long int

map<ll, int> m;

void modify(ll x, int k) {
  for (int i = 0; i < 4; i++) {
    m[x] += k;
    x = (x >> 10) | ((x & 1023) << 30);
  }
}

ll Hash(ll a, ll b, ll c, ll d) {
  return (a << 30) | (b << 20) | (c << 10) | d;
}

ll f[400][4], h[400], tmp[4];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> f[i][0] >> f[i][1] >> f[i][2] >> f[i][3];
    h[i] = Hash(f[i][0], f[i][1], f[i][2], f[i][3]);
    modify(h[i], 1);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    modify(h[i], -1);
    for (int j = i + 1; j < n; j++) {
      modify(h[j], -1);
      for (int k = 0; k < 4; k++) {
        tmp[0] = Hash(f[i][3], f[i][2], f[j][(k + 1) % 4], f[j][k]);
        tmp[1] = Hash(f[i][2], f[i][1], f[j][(k + 2) % 4], f[j][(k + 1) % 4]);
        tmp[2] = Hash(f[i][1], f[i][0], f[j][(k + 3) % 4], f[j][(k + 2) % 4]);
        tmp[3] = Hash(f[i][0], f[i][3], f[j][k], f[j][(k + 3) % 4]);
        ll z = 1;
        for (int l = 0; l < 4; l++) {
          z *= m[tmp[l]];
          modify(tmp[l], -1);
        }
        ans += z;
        for (int l = 0; l < 4; l++)
          modify(tmp[l], 1);
      }
      modify(h[j], 1);
    }
  }
  cout << ans << endl;
}


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

#define ll long long int

map<ll, int> m;

void modify(ll x, int k) {
  for (int i = 0; i < 4; i++) {
    m[x] += k;
    x = (x >> 10) | ((x & 1023) << 30);
  }
}

ll Hash(ll a, ll b, ll c, ll d) {
  return (a << 30) | (b << 20) | (c << 10) | d;
}

ll f[400][4], h[400], tmp[4];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> f[i][0] >> f[i][1] >> f[i][2] >> f[i][3];
    h[i] = Hash(f[i][0], f[i][1], f[i][2], f[i][3]);
    modify(h[i], 1);
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    modify(h[i], -1);
    for (int j = i + 1; j < n; j++) {
      modify(h[j], -1);
      for (int k = 0; k < 4; k++) {
        tmp[0] = Hash(f[i][3], f[i][2], f[j][(k + 1) % 4], f[j][k]);
        tmp[1] = Hash(f[i][2], f[i][1], f[j][(k + 2) % 4], f[j][(k + 1) % 4]);
        tmp[2] = Hash(f[i][1], f[i][0], f[j][(k + 3) % 4], f[j][(k + 2) % 4]);
        tmp[3] = Hash(f[i][0], f[i][3], f[j][k], f[j][(k + 3) % 4]);
        ll z = 1;
        if (m[tmp[0]] == 0 || m[tmp[1]] == 0 || m[tmp[2]] == 0 ||
            m[tmp[3]] == 0)
          continue;
        for (int l = 0; l < 4; l++) {
          z *= m[tmp[l]];
          modify(tmp[l], -1);
        }
        ans += z;
        for (int l = 0; l < 4; l++)
          modify(tmp[l], 1);
      }
      modify(h[j], 1);
    }
  }
  cout << ans << endl;
}
Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <map>
using namespace std;
typedef long long LL;
map<LL, int> f;
int c[405][4];
LL a[405];
LL Ch(LL x0, int x1, int x2, int x3) {
  return x0 << 30 | x1 << 20 | x2 << 10 | x3;
}
void add(LL x, int v) {
  for (int i = 0; i < 4; i++) {
    x = (x >> 10) + ((x & 1023) << 30);
    f[x] += v;
  }
}
int main() {
  int n;
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 4; j++)
      scanf("%d", &c[i][j]), a[i] = a[i] << 10 | c[i][j];
    add(a[i], 1);
  }
  LL ans = 0;
  for (int i = 1; i <= n - 5; i++) {
    add(a[i], -1);
    for (int j = i + 1; j <= n; j++) {
      add(a[j], -1);
      for (int k = 0; k < 4; k++) {
        LL A[4];
        A[0] = Ch(c[i][0], c[j][k], c[j][(k + 3) & 3], c[i][1]);
        A[1] = Ch(c[i][1], c[j][(k + 3) & 3], c[j][(k + 2) & 3], c[i][2]);
        A[2] = Ch(c[i][2], c[j][(k + 2) & 3], c[j][(k + 1) & 3], c[i][3]);
        A[3] = Ch(c[i][3], c[j][(k + 1) & 3], c[j][k], c[i][0]);
        LL cnt = 1;
        for (int l = 0; l < 4; l++)
          cnt *= f[A[l]], add(A[l], -1);
        for (int l = 0; l < 4; l++)
          add(A[l], 1);
        ans += cnt;
      }
      add(a[j], 1);
    }
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <cstdio>
#include <map>
using namespace std;
typedef long long LL;
map<LL, int> f;
int c[405][4];
LL a[405];
LL Ch(LL x0, int x1, int x2, int x3) {
  return x0 << 30 | x1 << 20 | x2 << 10 | x3;
}
void add(LL x, int v) {
  for (int i = 0; i < 4; i++) {
    x = (x >> 10) + ((x & 1023) << 30);
    f[x] += v;
  }
}
int main() {
  int n;
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 4; j++)
      scanf("%d", &c[i][j]), a[i] = a[i] << 10 | c[i][j];
    add(a[i], 1);
  }
  LL ans = 0;
  for (int i = 1; i <= n - 5; i++) {
    add(a[i], -1);
    for (int j = i + 1; j <= n; j++) {
      add(a[j], -1);
      for (int k = 0; k < 4; k++) {
        LL A[4];
        A[0] = Ch(c[i][0], c[j][k], c[j][(k + 3) & 3], c[i][1]);
        A[1] = Ch(c[i][1], c[j][(k + 3) & 3], c[j][(k + 2) & 3], c[i][2]);
        A[2] = Ch(c[i][2], c[j][(k + 2) & 3], c[j][(k + 1) & 3], c[i][3]);
        A[3] = Ch(c[i][3], c[j][(k + 1) & 3], c[j][k], c[i][0]);
        if (!f[A[0]] || !f[A[1]] || !f[A[2]] || !f[A[3]])
          continue;
        LL cnt = 1;
        for (int l = 0; l < 4; l++)
          cnt *= f[A[l]], add(A[l], -1);
        for (int l = 0; l < 4; l++)
          add(A[l], 1);
        ans += cnt;
      }
      add(a[j], 1);
    }
  }
  printf("%lld\n", ans);
}
Problem p03968 (C++)

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

typedef vector<int> VI;
const int N = 450;
VI a[N];
typedef long long ll;
ll ans;
map<VI, int> mp;
#define nxt(x) x.push_back(x[0]), x.erase(x.begin())
void add(VI x, int y) {
  for (int i = 0; i < 4; ++i, nxt(x))
    mp[x] += y;
}
int n;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 4; ++j) {
      int x;
      cin >> x;
      a[i].push_back(x);
    }
    add(a[i], 1);
  }
  for (int i = 0; i < n; ++i) {
    add(a[i], -1);
    for (int j = i + 1; j < n; ++j) {
      add(a[j], -1);
      for (int k = 0; k < 4; ++k) {
        VI x = {a[i][3], a[j][2], a[j][1], a[i][0]},
           y = {a[j][3], a[i][2], a[i][1], a[j][0]},
           z = {a[i][0], a[j][1], a[j][0], a[i][1]},
           w = {a[j][2], a[i][3], a[i][2], a[j][3]};
        ll res = 1;
        nxt(a[j]);
        res *= mp[x];
        add(x, -1);
        res *= mp[y];
        add(y, -1);
        res *= mp[z];
        add(z, -1);
        res *= mp[w];
        add(x, 1);
        add(y, 1);
        add(z, 1);
        ans += res;
      }
      add(a[j], 1);
    }
  }
  cout << ans << '\n';
  return 0;
}


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

typedef vector<int> VI;
const int N = 450;
VI a[N];
typedef long long ll;
ll ans;
map<VI, int> mp;
#define nxt(x) x.push_back(x[0]), x.erase(x.begin())
void add(VI x, int y) {
  for (int i = 0; i < 4; ++i, nxt(x))
    mp[x] += y;
}
int n;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 4; ++j) {
      int x;
      cin >> x;
      a[i].push_back(x);
    }
    add(a[i], 1);
  }
  for (int i = 0; i < n; ++i) {
    add(a[i], -1);
    for (int j = i + 1; j < n; ++j) {
      add(a[j], -1);
      for (int k = 0; k < 4; ++k) {
        VI x = {a[i][3], a[j][2], a[j][1], a[i][0]},
           y = {a[j][3], a[i][2], a[i][1], a[j][0]},
           z = {a[i][0], a[j][1], a[j][0], a[i][1]},
           w = {a[j][2], a[i][3], a[i][2], a[j][3]};
        ll res = 1;
        nxt(a[j]);
        if (!mp.count(x) || !mp.count(y) || !mp.count(z) || !mp.count(w))
          continue;
        res *= mp[x];
        add(x, -1);
        res *= mp[y];
        add(y, -1);
        res *= mp[z];
        add(z, -1);
        res *= mp[w];
        add(x, 1);
        add(y, 1);
        add(z, 1);
        ans += res;
      }
      add(a[j], 1);
    }
  }
  cout << ans << '\n';
  return 0;
}
Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <map>
#define ll long long
using namespace std;
const int N = 405;
int n, i, j, k, x, c[N][4];
ll a[N], v[4], ans;
ll num(int a, int b, int c, int d) {
  return ((ll)a << 30) + (b << 20) + (c << 10) + d;
}
map<ll, int> mp;
void add(ll x, int k) {
  for (int i = 4; i--; x = x >> 10 | (x & 1023) << 30)
    mp[x] += k;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; add(a[i], 1), i++)
    for (j = 0; j < 4; j++)
      scanf("%d", &c[i][j]), a[i] = (a[i] << 10) + c[i][j];
  for (i = 1; i <= n - 5; i++)
    for (add(a[i], -1), j = i + 1; j <= n; j++) {
      add(a[j], -1);
      for (k = 4; k--;) {
        v[0] = num(c[i][0], c[j][k], c[j][(k + 3) & 3], c[i][1]);
        v[1] = num(c[i][1], c[j][(k + 3) & 3], c[j][(k + 2) & 3], c[i][2]);
        v[2] = num(c[i][2], c[j][(k + 2) & 3], c[j][(k + 1) & 3], c[i][3]);
        v[3] = num(c[i][3], c[j][(k + 1) & 3], c[j][k], c[i][0]);
        ll res = 1;
        for (x = 4; x--;)
          res *= mp[v[x]], add(v[x], -1);
        for (ans += res, x = 4; x--;)
          add(v[x], 1);
      }
      add(a[j], 1);
    }
  printf("%lld", ans);
}

Fixed submission:
#include <cstdio>
#include <map>
#define ll long long
using namespace std;
const int N = 405;
int n, i, j, k, x, c[N][4];
ll a[N], v[4], ans;
ll num(int a, int b, int c, int d) {
  return ((ll)a << 30) + (b << 20) + (c << 10) + d;
}
map<ll, int> mp;
void add(ll x, int k) {
  for (int i = 4; i--; x = x >> 10 | (x & 1023) << 30)
    mp[x] += k;
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; add(a[i], 1), i++)
    for (j = 0; j < 4; j++)
      scanf("%d", &c[i][j]), a[i] = (a[i] << 10) + c[i][j];
  for (i = 1; i <= n - 5; i++)
    for (add(a[i], -1), j = i + 1; j <= n; j++) {
      add(a[j], -1);
      for (k = 4; k--;) {
        v[0] = num(c[i][0], c[j][k], c[j][(k + 3) & 3], c[i][1]);
        v[1] = num(c[i][1], c[j][(k + 3) & 3], c[j][(k + 2) & 3], c[i][2]);
        v[2] = num(c[i][2], c[j][(k + 2) & 3], c[j][(k + 1) & 3], c[i][3]);
        v[3] = num(c[i][3], c[j][(k + 1) & 3], c[j][k], c[i][0]);
        if (!mp[v[0]] || !mp[v[1]] || !mp[v[2]] || !mp[v[3]])
          continue;
        ll res = 1;
        for (x = 4; x--;)
          res *= mp[v[x]], add(v[x], -1);
        for (ans += res, x = 4; x--;)
          add(v[x], 1);
      }
      add(a[j], 1);
    }
  printf("%lld", ans);
}
Problem p03970 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  char s[17], t[17];
  int a = 0;
  strcpy("CODEFESTIVAL2016", t);
  for (int i = 0; i < 16; i++) {
    if (s[i] != t[i])
      a++;
  }
  cout << a;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  char s[17], t[17];
  int a = 0;
  cin >> s;
  strcpy(t, "CODEFESTIVAL2016");
  for (int i = 0; i < 16; i++) {
    if (s[i] != t[i])
      a++;
  }
  cout << a;
}
Problem p03971 (C++)

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

using namespace std;

char s[20] = "CODEFESTIVAL2016", t[20];

int main() {
  //    freopen ("in", "r", stdin);
  int n, a, b;
  cin >> n >> a >> b;
  scanf("%s", s + 1);
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'a') {
      if (c1 < a + b) {
        c1++;
        printf("Yes\n");
      } else {
        printf("No\n");
      }
    }
    if (s[i] == 'b') {
      if (c1 < a + b && c2 < b) {
        c2++;
        c1++;
        printf("Yes\n");
      } else {
        printf("No\n");
      }
    }
    if (s[i] == 'c')
      printf("No\n");
  }
}


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

using namespace std;

char s[100100] = "CODEFESTIVAL2016", t[20];

int main() {
  //    freopen ("in", "r", stdin);
  int n, a, b;
  cin >> n >> a >> b;
  scanf("%s", s + 1);
  int c1 = 0, c2 = 0;
  for (int i = 1; i <= n; i++) {
    if (s[i] == 'a') {
      if (c1 < a + b) {
        c1++;
        printf("Yes\n");
      } else {
        printf("No\n");
      }
    }
    if (s[i] == 'b') {
      if (c1 < a + b && c2 < b) {
        c2++;
        c1++;
        printf("Yes\n");
      } else {
        printf("No\n");
      }
    }
    if (s[i] == 'c')
      printf("No\n");
  }
}
Problem p03971 (C++)

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

int main() {
  int N, A, B;
  char S[20];

  cin >> N >> A >> B;
  cin >> S;

  int ab_rank = 0;
  int b_rank = 0;
  for (int i = 0; i < N; i++) {
    bool go = false;
    if (S[i] == 'a') {
      if (ab_rank < A + B) {
        go = true;
        ab_rank += 1;
      }
    } else if (S[i] == 'b') {
      if (ab_rank < A + B && b_rank < B) {
        go = true;
        ab_rank += 1;
        b_rank += 1;
      }
    }

    if (go) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }

    // cout << ":" << ab_rank << ":" << b_rank << ":" << S[i] << endl;
  }
}

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

int main() {
  int N, A, B;
  char S[100005];

  cin >> N >> A >> B;
  cin >> S;

  int ab_rank = 0;
  int b_rank = 0;
  for (int i = 0; i < N; i++) {
    bool go = false;
    if (S[i] == 'a') {
      if (ab_rank < A + B) {
        go = true;
        ab_rank += 1;
      }
    } else if (S[i] == 'b') {
      if (ab_rank < A + B && b_rank < B) {
        go = true;
        ab_rank += 1;
        b_rank += 1;
      }
    }

    if (go) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }

    // cout << ":" << ab_rank << ":" << b_rank << ":" << S[i] << endl;
  }
}
Problem p03971 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, sum = 0;
  int foreign = 0;
  char s[10010];
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == 'a') {
      if (a + b > sum) {
        cout << "Yes" << '\n';
        sum++;
      } else
        cout << "No" << '\n';
    } else if (s[i] == 'b') {
      if (a + b > sum && b > foreign) {
        cout << "Yes" << '\n';
        sum++;
        foreign++;
      } else
        cout << "No" << '\n';
    } else
      cout << "No" << '\n';
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a, b, sum = 0;
  int foreign = 0;
  char s[100010];
  cin >> n >> a >> b;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == 'a') {
      if (a + b > sum) {
        cout << "Yes" << '\n';
        sum++;
      } else
        cout << "No" << '\n';
    } else if (s[i] == 'b') {
      if (a + b > sum && b > foreign) {
        cout << "Yes" << '\n';
        sum++;
        foreign++;
      } else
        cout << "No" << '\n';
    } else
      cout << "No" << '\n';
  }
  return 0;
}
Problem p03971 (C++)

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

using namespace std;

int main() {
  int N, A, B;
  cin >> N >> A >> B;

  string S;
  cin >> S;

  bool isok[10005] = {};
  int a, b; // 国内の通過人数，海外の通過人数
  a = b = 0;

  for (int i = 0; i < N; ++i) {
    if (S[i] == 'a') {
      if (a + b < A + B) {
        isok[i] = true;
        a++;
      }
    } else if (S[i] == 'b') {
      if (a + b < A + B && b < B) {
        isok[i] = true;
        b++;
      }
    }
  }

  for (int i = 0; i < N; ++i)
    cout << (isok[i] ? "Yes" : "No") << endl;
}

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

using namespace std;

int main() {
  int N, A, B;
  cin >> N >> A >> B;

  string S;
  cin >> S;

  bool isok[100005] = {};
  int a, b; // 国内の通過人数，海外の通過人数
  a = b = 0;

  for (int i = 0; i < N; ++i) {
    if (S[i] == 'a') {
      if (a + b < A + B) {
        isok[i] = true;
        a++;
      }
    } else if (S[i] == 'b') {
      if (a + b < A + B && b < B) {
        isok[i] = true;
        b++;
      }
    }
  }

  for (int i = 0; i < N; ++i)
    cout << (isok[i] ? "Yes" : "No") << endl;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> T;
typedef unsigned long long ll;

int main() {
  int W, H;
  int p[100001];
  int q[100001];
  cin >> W >> H;
  for (int i = 0; i < W; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < H; i++) {
    cin >> q[i];
  }
  sort(p, p + W);
  sort(q, q + H);
  ll yoko = 0;
  ll tate = 0;
  ll ans = 0;
  ll nokori = (W + 1) * (H + 1) - 1;
  while (nokori > 0) {
    if (yoko < W && tate < H) {
      if (p[yoko] <= q[tate]) {
        ans += (H + 1 - tate) * p[yoko];
        yoko += 1;
        nokori -= (H + 1 - tate);
      } else {
        ans += (W + 1 - yoko) * q[tate];
        tate += 1;
        nokori -= (W + 1 - yoko);
      }
    } else if (yoko == W) {
      ans += (W + 1 - yoko) * q[tate];
      tate += 1;
      nokori -= (W + 1 - yoko);
    } else if (tate == H) {
      ans += (H + 1 - tate) * p[yoko];
      yoko += 1;
      nokori -= (H + 1 - tate);
    }
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> T;
typedef unsigned long long ll;

int main() {
  ll W, H;
  ll p[100001];
  ll q[100001];
  cin >> W >> H;
  for (int i = 0; i < W; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < H; i++) {
    cin >> q[i];
  }
  sort(p, p + W);
  sort(q, q + H);
  ll yoko = 0;
  ll tate = 0;
  ll ans = 0;
  ll nokori = (W + 1) * (H + 1) - 1;
  while (nokori > 0) {
    if (yoko < W && tate < H) {
      if (p[yoko] <= q[tate]) {
        ans += (H + 1 - tate) * p[yoko];
        yoko += 1;
        nokori -= (H + 1 - tate);
      } else {
        ans += (W + 1 - yoko) * q[tate];
        tate += 1;
        nokori -= (W + 1 - yoko);
      }
    } else if (yoko == W) {
      ans += (W + 1 - yoko) * q[tate];
      tate += 1;
      nokori -= (W + 1 - yoko);
    } else if (tate == H) {
      ans += (H + 1 - tate) * p[yoko];
      yoko += 1;
      nokori -= (H + 1 - tate);
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p03972 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// emplace_back next_permutation tie get

typedef long long LL;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPN(i, a, n) FOR((i), (a), (a) + (n))
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define PB push_back
#define MP make_pair
#define SE second
#define FI first
#define OUT(a) cout << (a) << endl;
#define M__ 100010

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL w, h, ans = 0;
  vector<pair<int, char>> a;

  cin >> w >> h;
  REP(i, w) {
    LL tmp;
    cin >> tmp;
    ans += tmp;
    a.push_back(make_pair(tmp, 'p'));
  }
  REP(i, h) {
    LL tmp;
    cin >> tmp;
    ans += tmp;
    a.push_back(make_pair(tmp, 'q'));
  }
  sort(a.begin(), a.end());

  while (!a.empty()) {
    LL x = a[0].first;
    char y = a[0].second;
    a.erase(a.begin());
    if (y == 'p') {
      ans += h * x;
      w--;
    } else {
      ans += w * x;
      h--;
    }
    // OUT(ans)
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// emplace_back next_permutation tie get

typedef long long LL;
typedef pair<LL, LL> PLL;
typedef vector<LL> VLL;
#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPN(i, a, n) FOR((i), (a), (a) + (n))
#define FOR(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define PB push_back
#define MP make_pair
#define SE second
#define FI first
#define OUT(a) cout << (a) << endl;
#define M__ 100010

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL w, h, ans = 0;
  vector<pair<int, char>> a;

  cin >> w >> h;
  REP(i, w) {
    LL tmp;
    cin >> tmp;
    ans += tmp;
    a.push_back(make_pair(tmp, 'p'));
  }
  REP(i, h) {
    LL tmp;
    cin >> tmp;
    ans += tmp;
    a.push_back(make_pair(tmp, 'q'));
  }
  sort(a.begin(), a.end());

  REP(i, a.size()) {
    LL x = a[i].first;
    char y = a[i].second;
    // a.erase(a.begin());
    if (y == 'p') {
      ans += h * x;
      w--;
    } else {
      ans += w * x;
      h--;
    }
    // OUT(ans)
  }
  cout << ans << endl;
  return 0;
}
Problem p03972 (C++)

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

using namespace std;

#define _for(i, j, N) for (int i = (j); i < (N); i++)
#define _rep(i, j, N) for (int i = (j); i <= (N); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int, int> pi;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  _for(i, 0, v.size()) os << v[i] << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (typename set<T>::iterator it = v.begin(); it != v.end(); it++)
    os << *it << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &v) {
  os << v.first << " " << v.second << endl;
  return os;
}

const int maxn = 1e5 + 5;
const LL INF = 1e18;
int W, H;

LL cw[maxn];
LL ch[maxn];

int main() {
  cin >> W >> H;
  _for(i, 0, W) { cin >> cw[i]; }
  cw[W] = INF;
  sort(cw, cw + W);
  _for(i, 0, H) { cin >> ch[i]; }
  ch[H] = INF;
  sort(ch, ch + H);

  int pi = 0;
  int pj = 0;
  int restj = H;
  int resti = W;

  LL need = (W + 1) * (LL)(H + 1) - 1;

  LL res = 0;
  while (need) {
    LL itake = (restj + 1) * cw[pi];
    LL jtake = (resti + 1) * ch[pj];
    if (itake < jtake) {
      res += itake;
      need -= restj + 1;
      pi++;
      resti--;
    } else {
      res += jtake;
      need -= resti + 1;
      pj++;
      restj--;
    }
    // cout << "need:"<<need<<" "<<"res:"<<res<< endl;
  }
  cout << res << endl;
  return 0;
}

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

using namespace std;

#define _for(i, j, N) for (int i = (j); i < (N); i++)
#define _rep(i, j, N) for (int i = (j); i <= (N); i++)
#define ALL(x) x.begin(), x.end()
#define pb push_back
#define mk make_pair

typedef long long LL;
typedef pair<int, int> pi;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  _for(i, 0, v.size()) os << v[i] << " ";
  return os;
}

template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (typename set<T>::iterator it = v.begin(); it != v.end(); it++)
    os << *it << " ";
  return os;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &v) {
  os << v.first << " " << v.second << endl;
  return os;
}

const int maxn = 1e5 + 5;
const LL INF = 1e18;
int W, H;

LL cw[maxn];
LL ch[maxn];

int main() {
  cin >> W >> H;
  _for(i, 0, W) { cin >> cw[i]; }
  cw[W] = INF;
  sort(cw, cw + W);
  _for(i, 0, H) { cin >> ch[i]; }
  ch[H] = INF;
  sort(ch, ch + H);

  int pi = 0;
  int pj = 0;
  int restj = H;
  int resti = W;

  LL need = (W + 1) * (LL)(H + 1) - 1;

  LL res = 0;
  while (need) {
    LL itake = (pi == W) ? INF : (restj + 1) * cw[pi];
    LL jtake = (pj == H) ? INF : (resti + 1) * ch[pj];
    if (cw[pi] < ch[pj]) {
      res += itake;
      need -= restj + 1;
      pi++;
      resti--;
    } else {
      res += jtake;
      need -= resti + 1;
      pj++;
      restj--;
    }
    // cout << "need:"<<need<<" "<<"res:"<<res<< endl;
  }
  cout << res << endl;
  return 0;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  int W, H;
  cin >> W >> H;
  VL P(W - 1), Q(H - 1);
  rep(i, W) cin >> P[i];
  rep(i, H) cin >> Q[i];
  vector<PLL> R(0);
  rep(i, W) R.pb(mp(P[i], 0));
  rep(i, H) R.pb(mp(Q[i], 1));
  sort(all(R));
  ll ans = 0;
  rep(i, R.size()) {
    if (R[i].second == 0) {
      ans += (H + 1) * R[i].first;
      W--;
    } else {
      ans += (W + 1) * R[i].first;
      H--;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<vector<int>> VVI;
typedef vector<vector<ll>> VVL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main() {
  int W, H;
  cin >> W >> H;
  VL P(W), Q(H);
  rep(i, W) cin >> P[i];
  rep(i, H) cin >> Q[i];
  vector<PLL> R(0);
  rep(i, W) R.pb(mp(P[i], 0));
  rep(i, H) R.pb(mp(Q[i], 1));
  sort(all(R));
  ll ans = 0;
  rep(i, R.size()) {
    if (R[i].second == 0) {
      ans += (H + 1) * R[i].first;
      W--;
    } else {
      ans += (W + 1) * R[i].first;
      H--;
    }
  }
  cout << ans << endl;
}
Problem p03972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MST(a, x) memset(a, x, sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 1005
#define M 51
#define INF 500
#define MOD 924844033
#define MOD2 1000000009
#define eps 1e-14
using namespace std;
inline int Max(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ b; }
inline int Min(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ a; }

int a[N], b[N];
ll sa[N];
int n, m;
const int cmp(const int x, const int y) { return x > y; }
int bs(int x) {
  int l = 0, r = n + 1;
  while (l < r - 1) {
    int mid = (l + r) >> 1;
    if (a[mid] >= x)
      l = mid;
    else
      r = mid;
  }
  return l;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> n >> m) {
    sa[0] = 0;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, m) cin >> b[i];
    sort(a + 1, a + n + 1, cmp);
    FOR(i, 1, n) sa[i] = sa[i - 1] + a[i];
    a[0] = INF;
    ll ans = 0;
    FOR(i, 1, m) {
      int p = bs(b[i]);
      ans += sa[n] - sa[p] + (ll)(p + 1) * (ll)b[i];
      // cout<<i<<" "<<p<<" "<<ans<<endl;
    }
    ans += sa[n];
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MST(a, x) memset(a, x, sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 100005
#define M 51
#define INF 500
#define MOD 924844033
#define MOD2 1000000009
#define eps 1e-14
using namespace std;
inline int Max(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ b; }
inline int Min(int a, int b) { return (((b - a) >> (32 - 1)) & (a ^ b)) ^ a; }

int a[N], b[N];
ll sa[N];
int n, m;
const int cmp(const int x, const int y) { return x > y; }
int bs(int x) {
  int l = 0, r = n + 1;
  while (l < r - 1) {
    int mid = (l + r) >> 1;
    if (a[mid] >= x)
      l = mid;
    else
      r = mid;
  }
  return l;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> n >> m) {
    sa[0] = 0;
    FOR(i, 1, n) cin >> a[i];
    FOR(i, 1, m) cin >> b[i];
    sort(a + 1, a + n + 1, cmp);
    FOR(i, 1, n) sa[i] = sa[i - 1] + a[i];
    a[0] = INF;
    ll ans = 0;
    FOR(i, 1, m) {
      int p = bs(b[i]);
      ans += sa[n] - sa[p] + (ll)(p + 1) * (ll)b[i];
      // cout<<i<<" "<<p<<" "<<ans<<endl;
    }
    ans += sa[n];
    cout << ans << endl;
  }
  return 0;
}
Problem p03972 (C++)

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

struct PR {
  int Par;
  int Rank;
};

struct route {
  long long Cost;
  int From;
  int To;
  int WH;
};

static PR PRW[100000], PRH[100000];

void init(int X, PR pr[]) {
  for (int i = 0; i <= X; i++) {
    pr[i].Par = i;
    pr[i].Rank = 0;
  }
}

int root(int X, PR pr[]) {
  if (pr[X].Par == X)
    return X;
  else
    return pr[X].Par = root(pr[X].Par, pr);
}

void unite(int X, int Y, PR pr[]) {
  X = root(X, pr), Y = root(Y, pr);
  if (pr[X].Rank < pr[Y].Rank)
    pr[X].Par = Y;
  else {
    pr[Y].Par = X;
    if (pr[X].Rank == pr[Y].Rank)
      pr[X].Rank++;
  }
}

bool same(int X, int Y, PR pr[]) { return root(X, pr) == root(Y, pr); }

int main() {
  static long long W, H, A, B, P, Q, ANS = 0;
  auto SORT = [](route R1, route R2) { return R1.Cost > R2.Cost; };
  priority_queue<route, vector<route>, decltype(SORT)> PQ(SORT);
  cin >> W >> H;
  A = W + 1, B = H + 1;
  init(W, PRW);
  init(H, PRH);
  for (int i = 0; i < W; i++)
    cin >> P, PQ.push(route{P, i, i + 1, 1});
  for (int i = 0; i < H; i++)
    cin >> Q, PQ.push(route{Q, i, i + 1, 2});
  while (!PQ.empty()) {
    route R = PQ.top();
    PQ.pop();
    if (R.WH == 1) {
      if (!same(R.From, R.To, PRW)) {
        ANS += R.Cost * B;
        A--;
        unite(R.From, R.To, PRW);
      }
    } else {
      if (!same(R.From, R.To, PRH)) {
        ANS += R.Cost * A;
        B--;
        unite(R.From, R.To, PRH);
      }
    }
  }
  cout << ANS;
}

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

struct PR {
  int Par;
  int Rank;
};

struct route {
  long long Cost;
  int From;
  int To;
  int WH;
};

static PR PRW[100010], PRH[100010];

void init(int X, PR pr[]) {
  for (int i = 0; i <= X; i++) {
    pr[i].Par = i;
    pr[i].Rank = 0;
  }
}

int root(int X, PR pr[]) {
  if (pr[X].Par == X)
    return X;
  else
    return pr[X].Par = root(pr[X].Par, pr);
}

void unite(int X, int Y, PR pr[]) {
  X = root(X, pr), Y = root(Y, pr);
  if (pr[X].Rank < pr[Y].Rank)
    pr[X].Par = Y;
  else {
    pr[Y].Par = X;
    if (pr[X].Rank == pr[Y].Rank)
      pr[X].Rank++;
  }
}

bool same(int X, int Y, PR pr[]) { return root(X, pr) == root(Y, pr); }

int main() {
  static long long W, H, A, B, P, Q, ANS = 0;
  auto SORT = [](route R1, route R2) { return R1.Cost > R2.Cost; };
  priority_queue<route, vector<route>, decltype(SORT)> PQ(SORT);
  cin >> W >> H;
  A = W + 1, B = H + 1;
  init(W, PRW);
  init(H, PRH);
  for (int i = 0; i < W; i++)
    cin >> P, PQ.push(route{P, i, i + 1, 1});
  for (int i = 0; i < H; i++)
    cin >> Q, PQ.push(route{Q, i, i + 1, 2});
  while (!PQ.empty()) {
    route R = PQ.top();
    PQ.pop();
    if (R.WH == 1) {
      if (!same(R.From, R.To, PRW)) {
        ANS += R.Cost * B;
        A--;
        unite(R.From, R.To, PRW);
      }
    } else {
      if (!same(R.From, R.To, PRH)) {
        ANS += R.Cost * A;
        B--;
        unite(R.From, R.To, PRH);
      }
    }
  }
  cout << ANS;
}