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

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1LL << 50

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<int> c, x;
    rep(i, m) {
      int a;
      cin >> a;
      c.pb(a);
    }
    rep(i, n) {
      int a;
      cin >> a;
      x.pb(a);
    }

    static ll dp[20100][260];
    rep(i, 30000) rep(j, 300) dp[i][j] = INF;
    dp[0][128] = 0;

    for (int i = 0; i < n; i++) {
      rep(j, 256) {
        rep(k, m) {
          int val = j + c[k];
          if (val > 255)
            val = 255;
          if (val < 0)
            val = 0;
          int pow2 = (x[i] - val) * (x[i] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
          //                    if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] =
          //                    min(dp[i][j],dp[i-1][j-c[k]]+pow2);
        }
      }
    }
    ll mini = INF;
    rep(i, 256) { mini = min(mini, dp[n][i]); }
    cout << mini << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1LL << 50

int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;
    vector<int> c, x;
    rep(i, m) {
      int a;
      cin >> a;
      c.pb(a);
    }
    rep(i, n) {
      int a;
      cin >> a;
      x.pb(a);
    }

    static ll dp[20100][260];
    rep(i, 20100) rep(j, 260) dp[i][j] = INF;
    dp[0][128] = 0;

    for (int i = 0; i < n; i++) {
      rep(j, 256) {
        rep(k, m) {
          int val = j + c[k];
          if (val > 255)
            val = 255;
          if (val < 0)
            val = 0;
          int pow2 = (x[i] - val) * (x[i] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
          //                    if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] =
          //                    min(dp[i][j],dp[i-1][j-c[k]]+pow2);
        }
      }
    }
    ll mini = INF;
    rep(i, 256) { mini = min(mini, dp[n][i]); }
    cout << mini << endl;
  }
}
Problem p01316 (C++)

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

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> code(m), value(n);
    for (int i = 0; i < m; i++)
      cin >> code[i];
    for (int i = 0; i < n; i++)
      cin >> value[i];
    vector<vector<int>> dp(256, vector<int>(n + 1, 1000000000));
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          int val = min(255, max(0, j + code[k]));
          dp[i + 1][val] = min(dp[i + 1][val],
                               dp[i][j] + (val - value[i]) * (val - value[i]));
        }
      }
    }
    int ans = 1000000000;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> code(m), value(n);
    for (int i = 0; i < m; i++)
      cin >> code[i];
    for (int i = 0; i < n; i++)
      cin >> value[i];
    vector<vector<int>> dp(n + 1, vector<int>(256, 1000000000));
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          int val = min(255, max(0, j + code[k]));
          dp[i + 1][val] = min(dp[i + 1][val],
                               dp[i][j] + (val - value[i]) * (val - value[i]));
        }
      }
    }
    int ans = 1000000000;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01316 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(j, n) for (int(j) = 0; j < (int)(n); ++j)
#define FOR(j, k, n) for (int(j) = (k); j < (int)(n); ++j)
#define FOREQ(j, k, n) for (int(j) = (k); j <= (int)(n); ++j)

#define MEMSET(z, val) memset(z, val, sizeof(z))

int n, m;
int x[30000];
int c[100];
int memo[30000][300];

inline ll square(ll x) { return x * x; }

ll calc(int depth, int color) {
  assert(0 <= color && color <= 255);
  if (memo[depth][color] != -1) {
    return memo[depth][color];
  }
  if (depth == n) {
    return memo[depth][color] = 0;
  }
  ll ret = 1LL << 60;
  REP(i, m) {
    int ncolor = color + c[i];
    ncolor = max(0, min(255, ncolor));
    int cost = square(abs(x[depth + 1] - ncolor));
    ret = min(ret, calc(depth + 1, ncolor) + cost);
  }
  return memo[depth][color] = ret;
}

int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    MEMSET(memo, -1);
    REP(i, m) { scanf("%d", &c[i]); }
    REP(i, n) { scanf("%d", &x[i + 1]); }
    printf("%lld\n", calc(0, 128));
  }
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

#define REP(j, n) for (int(j) = 0; j < (int)(n); ++j)
#define FOR(j, k, n) for (int(j) = (k); j < (int)(n); ++j)
#define FOREQ(j, k, n) for (int(j) = (k); j <= (int)(n); ++j)

#define MEMSET(z, val) memset(z, val, sizeof(z))

int n, m;
int x[30000];
int c[100];
int memo[20010][260];

inline ll square(ll x) { return x * x; }

ll calc(int depth, int color) {
  assert(0 <= color && color <= 255);
  if (memo[depth][color] != -1) {
    return memo[depth][color];
  }
  if (depth == n) {
    return memo[depth][color] = 0;
  }
  ll ret = 1LL << 60;
  REP(i, m) {
    int ncolor = color + c[i];
    ncolor = max(0, min(255, ncolor));
    int cost = square(abs(x[depth + 1] - ncolor));
    ret = min(ret, calc(depth + 1, ncolor) + cost);
  }
  return memo[depth][color] = ret;
}

int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    MEMSET(memo, -1);
    REP(i, m) { scanf("%d", &c[i]); }
    REP(i, n) { scanf("%d", &x[i + 1]); }
    printf("%lld\n", calc(0, 128));
  }
}
Problem p01317 (C++)

Buggy submission (status: Runtime Error):
// AOJ 2200 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2200)
#include <algorithm>
#include <cstdio>
#include <limits>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)

const int MAX_N = 200, MAX_R = 1000, INF = std::numeric_limits<int>::max() >> 4;

int N, M, R;
int land[MAX_N][MAX_N], sea[MAX_N][MAX_N];
int dp[MAX_R + 1][MAX_N];
int z[MAX_N];

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    rep(i, N) rep(j, N) land[i][j] = sea[i][j] = i == j ? 0 : INF;

    rep(i, M) {
      int x, y, t;
      char sl[2];

      scanf("%d%d%d%s", &x, &y, &t, sl);
      --x;
      --y;

      auto mat = *sl == 'L' ? land : sea;
      mat[x][y] = mat[y][x] = std::min(mat[x][y], t);
    }

    scanf("%d", &R);
    rep(i, R) scanf("%d", z + i), --z[i];

    rep(k, N) rep(i, N) rep(j, N) {
      land[i][j] = std::min(land[i][j], land[i][k] + land[k][j]);
      sea[i][j] = std::min(sea[i][j], sea[i][k] + sea[k][j]);
    }

    rep(i, R + 1) rep(j, N + 1) dp[i][j] = INF;

    dp[0][z[0]] = 0;
    rep(i, R - 1) rep(j, N) rep(k, N) {
      if (j != k)
        dp[i + 1][j] =
            std::min(dp[i + 1][j],
                     dp[i][k] + land[z[i]][k] + sea[k][j] + land[j][z[i + 1]]);
      else
        dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + land[z[i]][z[i + 1]]);
    }

    printf("%d\n", *std::min_element(dp[R - 1], dp[R - 1] + N));
  }

  return 0;
}

Fixed submission:
// AOJ 2200 (http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2200)
#include <algorithm>
#include <cstdio>
#include <limits>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)

const int MAX_N = 200, MAX_R = 1000, INF = std::numeric_limits<int>::max() >> 4;

int N, M, R;
int land[MAX_N][MAX_N], sea[MAX_N][MAX_N];
int dp[MAX_R + 1][MAX_N];
int z[MAX_R];

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    rep(i, N) rep(j, N) land[i][j] = sea[i][j] = i == j ? 0 : INF;

    rep(i, M) {
      int x, y, t;
      char sl[2];

      scanf("%d%d%d%s", &x, &y, &t, sl);
      --x;
      --y;

      auto mat = *sl == 'L' ? land : sea;
      mat[x][y] = mat[y][x] = std::min(mat[x][y], t);
    }

    scanf("%d", &R);
    rep(i, R) scanf("%d", z + i), --z[i];

    rep(k, N) rep(i, N) rep(j, N) {
      land[i][j] = std::min(land[i][j], land[i][k] + land[k][j]);
      sea[i][j] = std::min(sea[i][j], sea[i][k] + sea[k][j]);
    }

    rep(i, R + 1) rep(j, N + 1) dp[i][j] = INF;

    dp[0][z[0]] = 0;
    rep(i, R - 1) rep(j, N) rep(k, N) {
      if (j != k)
        dp[i + 1][j] =
            std::min(dp[i + 1][j],
                     dp[i][k] + land[z[i]][k] + sea[k][j] + land[j][z[i + 1]]);
      else
        dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j] + land[z[i]][z[i + 1]]);
    }

    printf("%d\n", *std::min_element(dp[R - 1], dp[R - 1] + N));
  }

  return 0;
}
Problem p01317 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
int v1[203][203], v2[203][203], a1, a2, a3, n, m;
int dp[2][203][203], t, x, pre;
char c;
int main() {
  while (cin >> n >> m, n) {
    int ans = 1e8;
    r(i, 2) r(j, 203) r(k, 203) dp[i][j][k] = 1e8;
    r(i, 203) r(j, 203) v1[i][j] = v2[j][i] = (i == j ? 0 : 1e8);
    r(i, n) {
      cin >> a1 >> a2 >> a3 >> c;
      a1--, a2--;
      if (c == 'L')
        v1[a1][a2] = v1[a2][a1] = a3;
      else
        v2[a1][a2] = v2[a2][a1] = a3;
    }
    r(k, n) r(i, n) r(j, n) {
      v1[i][j] = min(v1[i][j], v1[i][k] + v1[k][j]);
      v2[i][j] = min(v2[i][j], v2[i][k] + v2[k][j]);
    }
    cin >> t >> pre;
    pre--;
    dp[0][pre][pre] = 0;
    for (int k = 0; k < t - 1; k++) {
      cin >> x;
      x--;
      for (int i = 0; i < n; i++) {
        dp[(k + 1) % 2][x][i] =
            min(dp[(k + 1) % 2][x][i], dp[k % 2][pre][i] + v1[pre][x]);
        for (int j = 0; j < n; j++) {
          dp[(k + 1) % 2][x][j] =
              min(dp[(k + 1) % 2][x][j],
                  dp[k % 2][pre][i] + v1[pre][i] + v2[i][j] + v1[j][x]);
        }
      }
      r(i, 203) r(j, 203) dp[k % 2][i][j] = 1e8;
      pre = x;
    }
    r(i, 203) ans = min(ans, dp[(t - 1) % 2][pre][i]);
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
int v1[203][203], v2[203][203], a1, a2, a3, n, m;
int dp[2][203][203], t, x, pre;
char c;
int main() {
  while (cin >> n >> m, n) {
    int ans = 1e8;
    r(i, 2) r(j, 203) r(k, 203) dp[i][j][k] = 1e8;
    r(i, 203) r(j, 203) v1[i][j] = v2[j][i] = (i == j ? 0 : 1e8);
    r(i, m) {
      cin >> a1 >> a2 >> a3 >> c;
      a1--, a2--;
      if (c == 'L')
        v1[a1][a2] = v1[a2][a1] = a3;
      else
        v2[a1][a2] = v2[a2][a1] = a3;
    }
    r(k, n) r(i, n) r(j, n) {
      v1[i][j] = min(v1[i][j], v1[i][k] + v1[k][j]);
      v2[i][j] = min(v2[i][j], v2[i][k] + v2[k][j]);
    }
    cin >> t >> pre;
    pre--;
    dp[0][pre][pre] = 0;
    for (int k = 0; k < t - 1; k++) {
      cin >> x;
      x--;
      for (int i = 0; i < n; i++) {
        dp[(k + 1) % 2][x][i] =
            min(dp[(k + 1) % 2][x][i], dp[k % 2][pre][i] + v1[pre][x]);
        for (int j = 0; j < n; j++) {
          dp[(k + 1) % 2][x][j] =
              min(dp[(k + 1) % 2][x][j],
                  dp[k % 2][pre][i] + v1[pre][i] + v2[i][j] + v1[j][x]);
        }
      }
      r(i, 203) r(j, 203) dp[k % 2][i][j] = 1e8;
      pre = x;
    }
    r(i, 203) ans = min(ans, dp[(t - 1) % 2][pre][i]);
    cout << ans << endl;
  }
}
Problem p01321 (C++)

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

int main() {
  int a, b;
  while (scanf("%d%d", &a), a) {
    int MIN = 1 << 29, MAX = -(1 << 29);
    for (int c = 0; c < a; c++) {
      int s = 0;
      int e;
      for (int d = 0; d < 5; d++) {
        scanf("%d", &e);
        s += e;
      }
      MIN = min(MIN, s);
      MAX = max(MAX, s);
    }
    printf("%d %d\n", MAX, MIN);
  }
}

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

int main() {
  int a, b;
  while (scanf("%d", &a), a) {
    int MIN = 1 << 29, MAX = -(1 << 29);
    for (int c = 0; c < a; c++) {
      int s = 0;
      int e;
      for (int d = 0; d < 5; d++) {
        scanf("%d", &e);
        s += e;
      }
      MIN = min(MIN, s);
      MAX = max(MAX, s);
    }
    printf("%d %d\n", MAX, MIN);
  }
}
Problem p01321 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define INF 10000000
int mini(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int maxim(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  while (1) {
    int i, j, n, s, t, max = 0, min = 500;
    cin >> n;
    for (i = 0; i < n; i++) {
      t = 0;
      for (j = 0; j < 5; j++) {
        cin >> s;
        t += s;
      }
      max = maxim(max, t);
      min = mini(min, t);
    }
    cout << max << ' ' << min << endl;
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define INF 10000000
int mini(int a, int b) {
  if (a < b) {
    return a;
  } else {
    return b;
  }
}
int maxim(int a, int b) {
  if (a > b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  while (1) {
    int i, j, n, s, t, max = 0, min = 500;
    cin >> n;
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      t = 0;
      for (j = 0; j < 5; j++) {
        cin >> s;
        t += s;
      }
      max = maxim(max, t);
      min = mini(min, t);
    }
    cout << max << ' ' << min << endl;
  }
  return 0;
}
Problem p01322 (C++)

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

struct R {
  string y;
  int C;
};

int main() {

  int N, M;

  while (1) {

    struct R n[101];
    cin >> N >> M;
    for (int i = 0; i < N; i++) {
      cin >> n[i].y;
      cin >> n[i].C;
    }
    string str;
    int s = 0;
    for (int iii = 0; iii < M; iii++) {
      cin >> str;
      for (int i = 0; i < N; i++) {
        bool f = true;
        for (int j = 0; j < 8; j++)
          if ((n[i].y[j] != str[j]) && n[i].y[j] != '*') {
            f = false;
          } // cout<<n[i].y[j]<<"#"<<str[j]<<endl;}
        if (f == true)
          s += n[i].C;
      }
    }

    cout << s << endl;
  }
  return 0;
}

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

struct R {
  string y;
  int C;
};

int main() {

  int N, M;

  while (1) {

    struct R n[101];
    cin >> N;
    if (N == 0)
      break;
    cin >> M;
    for (int i = 0; i < N; i++) {
      cin >> n[i].y;
      cin >> n[i].C;
    }
    string str;
    int s = 0;
    for (int iii = 0; iii < M; iii++) {
      cin >> str;
      for (int i = 0; i < N; i++) {
        bool f = true;
        for (int j = 0; j < 8; j++)
          if ((n[i].y[j] != str[j]) && n[i].y[j] != '*') {
            f = false;
          } // cout<<n[i].y[j]<<"#"<<str[j]<<endl;}
        if (f == true)
          s += n[i].C;
      }
    }

    cout << s << endl;
  }
  return 0;
}
Problem p01322 (C++)

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

bool check(string a, string b) {
  for (int i = 0; i < 8; i++) {
    if (a[i] == '*')
      continue;
    if (a[i] != b[i])
      return 0;
  }
  return 1;
}

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    string a[101], b[101];
    int mon[101];
    ;
    for (int i = 0; i < n; i++)
      cin >> a[i] >> mon[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    long long ans = 0;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++)
        if (check(a[j], b[i]))
          ans += mon[j];
    cout << ans << endl;
  }

  return 0;
}

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

bool check(string a, string b) {
  for (int i = 0; i < 8; i++) {
    if (a[i] == '*')
      continue;
    if (a[i] != b[i])
      return 0;
  }
  return 1;
}

int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    string a[101], b[1001];
    int mon[1001];
    ;
    for (int i = 0; i < n; i++)
      cin >> a[i] >> mon[i];
    for (int i = 0; i < m; i++)
      cin >> b[i];

    long long ans = 0;
    for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++)
        if (check(a[j], b[i]))
          ans += mon[j];
    cout << ans << endl;
  }

  return 0;
}
Problem p01322 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
bool sc(string num, string in) {
  bool han = true;
  rep(i, 8) {
    if (num[i] == '*')
      continue;
    if (num[i] != in[i])
      han = false;
  }
  return han;
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<string> num(n);
    vector<int> money(m);
    rep(i, n) { cin >> num[i] >> money[i]; }
    int sum = 0;
    rep(i, m) {
      string in;
      cin >> in;
      rep(j, n) if (sc(num[j], in)) sum += money[j];
    }
    cout << sum << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb(in, tmp) in.push_back(tmp)
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
using namespace std;
bool sc(string num, string in) {
  bool han = true;
  rep(i, 8) {
    if (num[i] == '*')
      continue;
    if (num[i] != in[i])
      han = false;
  }
  return han;
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<string> num(n);
    vector<int> money(n);
    rep(i, n) { cin >> num[i] >> money[i]; }
    int sum = 0;
    rep(i, m) {
      string in;
      cin >> in;
      rep(j, n) if (sc(num[j], in)) sum += money[j];
    }
    cout << sum << endl;
  }
}
Problem p01324 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
map<string, vector<pair<string, int>>> b;
map<string, int> c;
int fn(string s, int p) {
  int i;
  if (c.count(s)) {
    if (c[s] != p)
      return 1;
    else
      return 0;
  } else {
    c[s] = p;
    for (i = 0; i < (int)b[s].size(); i++) {
      if (fn(b[s][i].first, p + b[s][i].second))
        return 1;
    }
    c.erase(s);
    return 0;
  }
}
int main() {
  int i;
  int n;
  while (cin >> n, n) {
    set<string> a;
    b.clear();
    for (i = 0; i < n; i++) {
      string s, t;
      char c;
      int p;
      cin >> s >> s >> c >> c >> c >> c >> p >> t;
      a.insert(s);
      a.insert(t);
      b[s].push_back(make_pair(t, -p));
      b[t].push_back(make_pair(s, p));
    }
    set<string>::iterator it;
    for (it = a.begin(); it != a.end(); it++) {
      c.clear();
      if (fn(*it, 0))
        break;
    }
    if (it != a.end())
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
map<string, vector<pair<string, int>>> b;
map<string, int> c;
int fn(string s, int p) {
  int i;
  if (c.count(s)) {
    if (c[s] != p)
      return 1;
    else
      return 0;
  } else {
    c[s] = p;
    for (i = 0; i < (int)b[s].size(); i++) {
      if (fn(b[s][i].first, p + b[s][i].second))
        return 1;
    }
    return 0;
  }
}
int main() {
  int i;
  int n;
  while (cin >> n, n) {
    set<string> a;
    b.clear();
    for (i = 0; i < n; i++) {
      string s, t;
      char c;
      int p;
      cin >> s >> s >> c >> c >> c >> c >> p >> t;
      a.insert(s);
      a.insert(t);
      b[s].push_back(make_pair(t, -p));
      b[t].push_back(make_pair(s, p));
    }
    set<string>::iterator it;
    for (it = a.begin(); it != a.end(); it++) {
      c.clear();
      if (fn(*it, 0))
        break;
    }
    if (it != a.end())
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
  return 0;
}
Problem p01324 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
char in[30];
int UF[300];
int w[300];
pair<int, int> FIND(int a) {
  if (UF[a] < 0)
    return make_pair(a, 0);
  pair<int, int> ret = FIND(UF[a]);
  ret.second += w[a];
  return ret;
}
bool ok = true;
void UNION(int a, int b, int c) {
  pair<int, int> A = FIND(a);
  pair<int, int> B = FIND(b);
  int d = A.first;
  int e = B.first;
  int f = A.second + c - B.second;
  if (d == e) {
    if (B.second - c != A.second)
      ok = false;
    return;
  }
  UF[a] += UF[b];
  UF[b] = a;
  w[b] = f;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    map<string, int> m;
    int sz = 0;
    ok = true;
    for (int i = 0; i < 300; i++) {
      UF[i] = -1;
      w[i] = 0;
    }
    for (int i = 0; i < a; i++) {
      scanf("%s%s", in, in);
      string A = in;
      int C = 0;
      scanf("%s%s", in, in);
      if (in[1] == 0)
        C = 0;
      else {
        sscanf(in, "%d^%d", &C, &C);
      }
      scanf("%s", in);
      string B = in;
      int p, q;
      if (!m.count(A)) {
        p = sz;
        m[A] = sz++;
      } else
        p = m[A];
      if (!m.count(B)) {
        q = sz;
        m[B] = sz++;
      } else
        q = m[B];
      UNION(p, q, C);
      //	if(!ok)printf("%d\n",i);
    }
    if (ok)
      printf("Yes\n");
    else
      printf("No\n");
  }
}

Fixed submission:
#include <algorithm>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
char in[30];
int UF[300];
int w[300];
pair<int, int> FIND(int a) {
  if (UF[a] < 0)
    return make_pair(a, 0);
  pair<int, int> ret = FIND(UF[a]);
  ret.second += w[a];
  return ret;
}
bool ok = true;
void UNION(int a, int b, int c) {
  pair<int, int> A = FIND(a);
  pair<int, int> B = FIND(b);
  int d = A.first;
  int e = B.first;
  int f = A.second + c - B.second;
  if (d == e) {
    if (B.second - c != A.second)
      ok = false;
    return;
  }
  UF[d] += UF[d];
  UF[e] = d;
  w[e] = f;
}
int main() {
  int a;
  while (scanf("%d", &a), a) {
    map<string, int> m;
    int sz = 0;
    ok = true;
    for (int i = 0; i < 300; i++) {
      UF[i] = -1;
      w[i] = 0;
    }
    for (int i = 0; i < a; i++) {
      scanf("%s%s", in, in);
      string A = in;
      int C = 0;
      scanf("%s%s", in, in);
      if (in[1] == 0)
        C = 0;
      else {
        sscanf(in, "%d^%d", &C, &C);
      }
      scanf("%s", in);
      string B = in;
      int p, q;
      if (!m.count(A)) {
        p = sz;
        m[A] = sz++;
      } else
        p = m[A];
      if (!m.count(B)) {
        q = sz;
        m[B] = sz++;
      } else
        q = m[B];
      UNION(p, q, C);
      //	if(!ok)printf("%d\n",i);
    }
    if (ok)
      printf("Yes\n");
    else
      printf("No\n");
  }
}
Problem p01324 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX_M = 101;
const int INF = 1 << 24;
map<string, int> id;
int G[MAX_M][MAX_M];
int M;

int getId(string s) {
  if (id.find(s) == id.end()) {
    id[s] = M++;
  }
  return id[s];
}

bool solve() {
  for (int k = 0; k < M; ++k) {
    for (int s = 0; s < M; ++s) {
      for (int d = 0; d < M; ++d) {
        if (G[s][k] == INF || G[k][d] == INF)
          continue;
        if (G[s][d] == INF) {
          G[s][d] = G[s][k] + G[k][d];
        } else if (G[s][d] != G[s][k] + G[k][d]) {
          return false;
        }
      }
    }
  }
  return true;
}

int main() {
  int N;
  while (cin >> N && N) {
    M = 0;
    id.clear();
    fill(G[0], G[MAX_M], INF);
    for (int i = 0; i < N; ++i) {
      string in[5];
      for (int j = 0; j < 5; ++j)
        cin >> in[j];
      int a = getId(in[1]);
      int b = getId(in[4]);
      int cost = atoi((in[3].substr(3)).c_str());
      G[a][b] = cost;
      G[b][a] = -cost;
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
const int MAX_M = 201;
const int INF = 1 << 24;
map<string, int> id;
int G[MAX_M][MAX_M];
int M;

int getId(string s) {
  if (id.find(s) == id.end()) {
    id[s] = M++;
  }
  return id[s];
}

bool solve() {
  for (int k = 0; k < M; ++k) {
    for (int s = 0; s < M; ++s) {
      for (int d = 0; d < M; ++d) {
        if (G[s][k] == INF || G[k][d] == INF)
          continue;
        if (G[s][d] == INF) {
          G[s][d] = G[s][k] + G[k][d];
        } else if (G[s][d] != G[s][k] + G[k][d]) {
          return false;
        }
      }
    }
  }
  return true;
}

int main() {
  int N;
  while (cin >> N && N) {
    M = 0;
    id.clear();
    fill(G[0], G[MAX_M], INF);
    for (int i = 0; i < N; ++i) {
      string in[5];
      for (int j = 0; j < 5; ++j)
        cin >> in[j];
      int a = getId(in[1]);
      int b = getId(in[4]);
      int cost = atoi((in[3].substr(3)).c_str());
      G[a][b] = cost;
      G[b][a] = -cost;
    }
    cout << (solve() ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01334 (C++)

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

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#ifdef Debug
#define dump(x) cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif

int par[100];
int Rank[100];

void init(int n) {
  rep(i, n) {
    par[i] = i;
    Rank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    pii data[110][110] = {};
    rep(i, n) rep(j, n) cin >> data[i][j].first >> data[i][j].second;

    init(n * n);

    rep(i, n) {
      rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
    }
    bool used[10000] = {};
    int sum = 0;
    rep(i, n * n) {
      int aa = find(i);
      if (used[aa] == false) {
        sum++;
        used[aa] = true;
      }
    }
    cout << sum << endl;
  }
  return 0;
}

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

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#ifdef Debug
#define dump(x) cerr << #x << " = " << (x) << endl
#else
#define dump(x)
#endif

int par[10000];
int Rank[10000];

void init(int n) {
  rep(i, n) {
    par[i] = i;
    Rank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (Rank[x] < Rank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

int main() {
  int n;
  while (cin >> n) {
    if (n == 0)
      break;
    pii data[110][110] = {};
    rep(i, n) rep(j, n) cin >> data[i][j].first >> data[i][j].second;

    init(n * n);

    rep(i, n) {
      rep(j, n) { unite(n * i + j, n * data[i][j].second + data[i][j].first); }
    }
    bool used[10000] = {};
    int sum = 0;
    rep(i, n * n) {
      int aa = find(i);
      if (used[aa] == false) {
        sum++;
        used[aa] = true;
      }
    }
    cout << sum << endl;
  }
  return 0;
}
Problem p01341 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <vector>
#define MAX_N 1005
#define MAX_R 20005
using namespace std;

typedef struct edge {
  int from, to, cost;
} edge;

bool mcmp(edge x, edge y) { return x.cost < y.cost; }

int uft[MAX_N];
vector<edge> e;

void init(int n) {
  int i;
  for (i = 0; i <= n; i++)
    uft[i] = i;
}

int parent(int n) {
  if (uft[n] == n)
    return n;
  else
    return uft[n] = parent(uft[n]);
}

void unit(int p, int q) { uft[parent(p)] = parent(q); }

int same(int p, int q) { return parent(p) == parent(q); }

int kruskal(int n, int r) {
  int res = 0;
  sort(e.begin(), e.end(), mcmp);
  init(n);
  for (int i = 0; i < r; i++) {
    edge tmp = e[i];
    if (!same(tmp.from, tmp.to)) {
      res += tmp.cost;
      unit(tmp.from, tmp.to);
      e[i].cost = 0;
    }
  }
  return res;
}

int piles[10001][2];
int m_distance(int f, int t) {
  return pow((double)piles[f][0] - piles[t][0], 2) +
         pow((double)piles[f][1] - piles[t][1], 2);
}

int main() {
  int N, M;
  int all_cost = 0;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &piles[i][0], &piles[i][1]);
  for (int j = 0; j < M; j++) {
    edge tmp;
    scanf("%d%d", &tmp.from, &tmp.to);
    tmp.cost = -m_distance(tmp.from, tmp.to);
    e.push_back(tmp);
  }
  kruskal(N, M);
  double res = 0.0;
  for (int i = 0; i < M; i++)
    res += sqrt(-(double)e[i].cost);
  printf("%.3lf\n", res);
  e.clear();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <stdio.h>
#include <vector>
#define MAX_N 10005
using namespace std;

typedef struct edge {
  int from, to, cost;
} edge;

bool mcmp(edge x, edge y) { return x.cost < y.cost; }

int uft[MAX_N];
vector<edge> e;

void init(int n) {
  int i;
  for (i = 0; i <= n; i++)
    uft[i] = i;
}

int parent(int n) {
  if (uft[n] == n)
    return n;
  else
    return uft[n] = parent(uft[n]);
}

void unit(int p, int q) { uft[parent(p)] = parent(q); }

int same(int p, int q) { return parent(p) == parent(q); }

int kruskal(int n, int r) {
  int res = 0;
  sort(e.begin(), e.end(), mcmp);
  init(n);
  for (int i = 0; i < r; i++) {
    edge tmp = e[i];
    if (!same(tmp.from, tmp.to)) {
      res += tmp.cost;
      unit(tmp.from, tmp.to);
      e[i].cost = 0;
    }
  }
  return res;
}

int piles[10001][2];
int m_distance(int f, int t) {
  return pow((double)piles[f][0] - piles[t][0], 2) +
         pow((double)piles[f][1] - piles[t][1], 2);
}

int main() {
  int N, M;
  int all_cost = 0;
  scanf("%d%d", &N, &M);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &piles[i][0], &piles[i][1]);
  for (int j = 0; j < M; j++) {
    edge tmp;
    scanf("%d%d", &tmp.from, &tmp.to);
    tmp.cost = -m_distance(tmp.from, tmp.to);
    e.push_back(tmp);
  }
  kruskal(N, M);
  double res = 0.0;
  for (int i = 0; i < M; i++)
    res += sqrt(-(double)e[i].cost);
  printf("%.3lf\n", res);
  e.clear();
  return 0;
}
Problem p01341 (C++)

Buggy submission (status: Runtime Error):
// kruskal tree
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
#define M 10000
int parent[M], a[M], b[M];
pair<double, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  double s = 0;
  int i, n, m;
  scanf("%d%d", &n, &m);
  vector<pair<double, double>> v(n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &v[i].first, &v[i].second);
  for (i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y), x--, y--;
    a[i] = x, b[i] = y;
    node[i].first = -hypot(v[x].first - v[y].first, v[x].second - v[y].second);
    s -= node[i].first;
    node[i].second = i;
  }
  sort(node, node + m);
  for (i = 0; i < n; i++)
    parent[i] = i;
  for (i = 0; i < m; i++)
    if (unite(a[node[i].second], b[node[i].second]))
      s += node[i].first;
  printf("%f\n", s);
}

Fixed submission:
// kruskal tree
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
#define M 99999
int parent[M], a[M], b[M];
pair<double, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  double s = 0;
  int i, n, m;
  scanf("%d%d", &n, &m);
  vector<pair<double, double>> v(n);
  for (i = 0; i < n; i++)
    scanf("%lf%lf", &v[i].first, &v[i].second);
  for (i = 0; i < m; i++) {
    int x, y;
    scanf("%d%d", &x, &y), x--, y--;
    a[i] = x, b[i] = y;
    node[i].first = -hypot(v[x].first - v[y].first, v[x].second - v[y].second);
    s -= node[i].first;
    node[i].second = i;
  }
  sort(node, node + m);
  for (i = 0; i < n; i++)
    parent[i] = i;
  for (i = 0; i < m; i++)
    if (unite(a[node[i].second], b[node[i].second]))
      s += node[i].first;
  printf("%f\n", s);
}
Problem p01341 (C++)

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

struct Way {
  int p, q, cost;
  Way() {}
  Way(int _p, int _q, int _cost) {
    p = _p;
    q = _q;
    cost = _cost;
  }
  bool operator<(const Way &a) const { return cost > a.cost; }
};

struct Node {
  int x, y;
};

int par[10000], _rank[10000];

void init(int n) {
  for (int i = 0; i < n; i++)
    par[i] = i, _rank[i] = 1;
}

int find(int a) {
  if (par[a] == a)
    return a;
  else
    return par[a] = find(par[a]);
}

void unite(int _a, int _b) {
  int a = find(_a);
  int b = find(_b);
  if (a == b)
    return;
  if (_rank[a] > _rank[b]) {
    par[b] = a;
  } else {
    par[a] = b;
    if (_rank[a] == _rank[b])
      _rank[b]++;
  }
}

int main() {
  int N, M;
  Node node[10000];
  Way way[10000000];
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> node[i].x >> node[i].y;
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> way[i].p >> way[i].q;
    a = node[--way[i].p].x - node[--way[i].q].x;
    b = node[way[i].p].y - node[way[i].q].y;
    way[i].cost = a * a + b * b;
    // printf("\t%d\n",way[i].cost);
  }

  double ans = 0;
  sort(way, way + M);
  init(N);
  for (int i = 0; i < M; i++) {
    int a = find(way[i].p), b = find(way[i].q);
    if (a != b)
      unite(a, b);
    else
      ans += sqrt(way[i].cost);
  }
  printf("%lf\n", ans);
}

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

struct Way {
  int p, q, cost;
  Way() {}
  Way(int _p, int _q, int _cost) {
    p = _p;
    q = _q;
    cost = _cost;
  }
  bool operator<(const Way &a) const { return cost > a.cost; }
};

struct Node {
  int x, y;
};

int par[10000], _rank[10000];

void init(int n) {
  for (int i = 0; i < n; i++)
    par[i] = i, _rank[i] = 1;
}

int find(int a) {
  if (par[a] == a)
    return a;
  else
    return par[a] = find(par[a]);
}

void unite(int _a, int _b) {
  int a = find(_a);
  int b = find(_b);
  if (a == b)
    return;
  if (_rank[a] > _rank[b]) {
    par[b] = a;
  } else {
    par[a] = b;
    if (_rank[a] == _rank[b])
      _rank[b]++;
  }
}

int main() {
  int N, M;
  Node node[10000];
  Way way[100000];
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> node[i].x >> node[i].y;
  }
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> way[i].p >> way[i].q;
    a = node[--way[i].p].x - node[--way[i].q].x;
    b = node[way[i].p].y - node[way[i].q].y;
    way[i].cost = a * a + b * b;
    // printf("\t%d\n",way[i].cost);
  }

  double ans = 0;
  sort(way, way + M);
  init(N);
  for (int i = 0; i < M; i++) {
    int a = find(way[i].p), b = find(way[i].q);
    if (a != b)
      unite(a, b);
    else
      ans += sqrt(way[i].cost);
  }
  printf("%lf\n", ans);
}
Problem p01341 (C++)

Buggy submission (status: Runtime Error):
/*
 * @Author: 王文宇
 * @Date:   2018-03-14 23:59:01
 * @Last Modified by:   王文宇
 * @Last Modified time: 2018-03-15 00:12:07
 */
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10007;
#define _for(i, a, b) for (int i = a; i <= b; i++)
int n, m, fa[maxn];
struct node {
  int x, y;
  double length;
} a[maxn];
struct point {
  int x, y;
} b[maxn];
bool cmp(node a, node b) { return a.length > b.length; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
double get(int x1, int x2, int y1, int y2) {
  double xx = (double)(x1 - x2) * (double)(x1 - x2);
  double yy = (double)(y1 - y2) * (double)(y1 - y2);
  return sqrt(xx + yy);
}
int main(int argc, char const *argv[]) {
  cin >> n >> m;
  _for(i, 1, n) {
    fa[i] = i;
    int x, y;
    cin >> b[i].x >> b[i].y;
  }
  double sum = 0.0;
  _for(i, 1, m) {
    int x, y;
    cin >> x >> y;
    a[i].x = x;
    a[i].y = y;
    a[i].length = get(b[x].x, b[y].x, b[x].y, b[y].y);
    sum += a[i].length;
  }
  sort(a + 1, a + 1 + m, cmp);
  _for(i, 1, m) {
    int p = find(a[i].x);
    int q = find(a[i].y);
    if (p != q) {
      sum -= a[i].length;
      fa[p] = q;
    }
  }
  printf("%.3f\n", sum);
  return 0;
}


Fixed submission:
/*
 * @Author: 王文宇
 * @Date:   2018-03-14 23:59:01
 * @Last Modified by:   王文宇
 * @Last Modified time: 2018-03-15 00:12:07
 */
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000007;
#define _for(i, a, b) for (int i = a; i <= b; i++)
int n, m, fa[maxn];
struct node {
  int x, y;
  double length;
} a[maxn];
struct point {
  int x, y;
} b[maxn];
bool cmp(node a, node b) { return a.length > b.length; }
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
double get(int x1, int x2, int y1, int y2) {
  double xx = (double)(x1 - x2) * (double)(x1 - x2);
  double yy = (double)(y1 - y2) * (double)(y1 - y2);
  return sqrt(xx + yy);
}
int main(int argc, char const *argv[]) {
  cin >> n >> m;
  _for(i, 1, n) {
    fa[i] = i;
    int x, y;
    cin >> b[i].x >> b[i].y;
  }
  double sum = 0.0;
  _for(i, 1, m) {
    int x, y;
    cin >> x >> y;
    a[i].x = x;
    a[i].y = y;
    a[i].length = get(b[x].x, b[y].x, b[x].y, b[y].y);
    sum += a[i].length;
  }
  sort(a + 1, a + 1 + m, cmp);
  _for(i, 1, m) {
    int p = find(a[i].x);
    int q = find(a[i].y);
    if (p != q) {
      sum -= a[i].length;
      fa[p] = q;
    }
  }
  printf("%.3f\n", sum);
  return 0;
}
Problem p01351 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <queue>
#include <set>

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

typedef long long ll;

using namespace std;

int check(int a, int m) {
  vector<int> visit(m);
  int now = 0;

  while (!visit[now]) {
    visit[now] = 1;
    now = (now + a) % m;
  }

  return accumulate(visit.begin(), visit.end(), 0);
}

int main() {
  int m = getInt();
  int n = getInt();
  int a = getInt();
  int b = getInt();
  int c = getInt();
  int d = getInt();

  a %= m;
  b %= n;
  c %= m;
  d %= n;

  int acnt = check(a, m);
  int bcnt = check(b, n);

  ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
  ll ans2 = 0;

  vector<int> h(m, -1);
  int hh = 0;
  int xx = 0;
  int yy = 0;

  while (h[xx] == -1) {
    h[xx] = yy;
    xx = (xx + a) % m;
    yy = (yy + b) % n;
    hh++;
  }

  int ym = ans1 / hh;
  int yd = n / ym;

  xx = c;
  yy = d;

  while (true) {
    ans2++;

    if (h[xx] != -1) {
      if ((yy - h[xx] + n) % yd == 0)
        break;
    }

    xx = (xx + c) % m;
    yy = (yy + d) % n;
  }

  printf("%lld\n", ans1 * ans2 - 1);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <queue>
#include <set>

inline int getInt() {
  int s;
  scanf("%d", &s);
  return s;
}

typedef long long ll;

using namespace std;

int check(int a, int m) {
  vector<int> visit(m);
  int now = 0;

  while (!visit[now]) {
    visit[now] = 1;
    now = (now + a) % m;
  }

  return accumulate(visit.begin(), visit.end(), 0);
}

int main() {
  int m = getInt();
  int n = getInt();
  int a = getInt();
  int b = getInt();
  int c = getInt();
  int d = getInt();

  a %= m;
  b %= n;
  c %= m;
  d %= n;

  int acnt = check(a, m);
  int bcnt = check(b, n);

  ll ans1 = ((ll)acnt * bcnt) / __gcd(acnt, bcnt);
  ll ans2 = 0;

  vector<int> h(m, -1);
  int hh = 0;
  int xx = 0;
  int yy = 0;

  while (h[xx] == -1) {
    h[xx] = yy;
    xx = (xx + a) % m;
    yy = (yy + b) % n;
    hh++;
  }

  int ym = ans1 / hh;
  int yd = n / ym;

  xx = c;
  yy = d;

  while (true) {
    ans2++;

    if (ans2 == 1000 * n) {
      int ccnt = check(c, m);
      int dcnt = check(d, n);
      ans2 = ((ll)ccnt * dcnt) / __gcd(ccnt, dcnt);
      break;
    }

    if (h[xx] != -1) {
      if ((yy - h[xx] + n) % yd == 0)
        break;
    }

    xx = (xx + c) % m;
    yy = (yy + d) % n;
  }

  printf("%lld\n", ans1 * ans2 - 1);

  return 0;
}
Problem p01353 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;

i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];

i64 ceil(i64 A, i64 B) { return A / B + min(1LL, A % B); }

int main() {
  cin >> n;
  vector<int> vec;
  rep(i, 0, n) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i == 0)
      continue;

    // cant
    if (a[0] <= d[i] && a[i] > d[0]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[0] > d[i]) {
      vec.push_back(i);
      A[i] = (ceil(h[i], a[0] - d[i]));
      B[i] = (max(0LL, a[i] - d[0]));
    }
  }

  auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };

  sort(vec.begin(), vec.end(), ope);

  i64 HP = 0;
  i64 cou = 0;

  rep(i, 0, vec.size() - 1) {
    int index = vec[i];
    cou += A[index];
    HP += cou * B[index];
    if (s[index] < s[0])
      HP -= B[index];
    if (h[0] <= HP) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << HP << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long i64;
typedef long double ld;
#define rep(i, s, e) for (int i = (s); i <= (e); i++)

int n;

i64 h[40404];
i64 a[40404];
i64 d[40404];
i64 s[40404];
i64 A[40404];
i64 B[40404];

i64 ceil(i64 A, i64 B) { return A / B + min(1LL, A % B); }

int main() {
  cin >> n;
  vector<int> vec;
  rep(i, 0, n) {
    cin >> h[i] >> a[i] >> d[i] >> s[i];
    if (i == 0)
      continue;

    // cant
    if (a[0] <= d[i] && a[i] > d[0]) {
      cout << -1 << endl;
      return 0;
    }
    if (a[0] > d[i]) {
      vec.push_back(i);
      A[i] = (ceil(h[i], a[0] - d[i]));
      B[i] = (max(0LL, a[i] - d[0]));
    }
  }

  auto ope = [&](int i, int j) { return A[i] * B[j] < A[j] * B[i]; };

  sort(vec.begin(), vec.end(), ope);

  i64 HP = 0;
  i64 cou = 0;

  rep(i, 0, (int)vec.size() - 1) {
    int index = vec[i];
    cou += A[index];
    HP += cou * B[index];
    if (s[index] < s[0])
      HP -= B[index];
    if (h[0] <= HP) {
      cout << -1 << endl;
      return 0;
    }
  }

  cout << HP << endl;
  return 0;
}
Problem p01353 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;

struct info {
  int h, a;
  double rate;
  info(int h, int a, double r) : h(h), a(a), rate(r) {}
  info() {}
};
bool comp(const info &a, const info &b) { return a.rate > b.rate; }

int main() {
  int n;
  cin >> n;

  lli h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<info> e(n);
  lli sum = 0, damage = 0;
  for (int i = 0; i < n; i++) {
    int eh, ea, ed, es;
    cin >> eh >> ea >> ed >> es;
    ea -= d;
    if (ea <= 0) {
      e[i] = info(1, 0, 0);
      continue;
    }
    if (es > s) {
      damage += ea;
    }
    if (a - ed <= 0) {
      h = 0;
      continue;
    }
    eh = (eh + (a - ed) - 1) / (a - ed);
    e[i] = info(eh, ea, (double)ea / eh);
    sum += ea;
  }
  if (h == 0) {
    cout << -1 << endl;
    return 0;
  }

  sort(e.begin(), e.end(), comp);
  for (int i = 0; i < (int)e.size(); i++) {
    int limit = (h - damage) / sum;
    if (e[i].h > limit) {
      h = 0;
      break;
    }
    damage += sum * (e[i].h - 1);
    sum -= e[i].a;
    damage += sum;
  }
  if (h <= damage) {
    cout << -1 << endl;
  } else {
    cout << damage << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long int lli;

struct info {
  int h, a;
  double rate;
  info(int h, int a, double r) : h(h), a(a), rate(r) {}
  info() {}
};
bool comp(const info &a, const info &b) { return a.rate > b.rate; }

int main() {
  int n;
  cin >> n;

  lli h, a, d, s;
  cin >> h >> a >> d >> s;
  vector<info> e(n);
  lli sum = 0, damage = 0;
  for (int i = 0; i < n; i++) {
    int eh, ea, ed, es;
    cin >> eh >> ea >> ed >> es;
    ea -= d;
    if (ea <= 0) {
      e[i] = info(1, 0, 0);
      continue;
    }
    if (es > s) {
      damage += ea;
    }
    if (a - ed <= 0) {
      h = 0;
      continue;
    }
    eh = (eh + (a - ed) - 1) / (a - ed);
    e[i] = info(eh, ea, (double)ea / eh);
    sum += ea;
  }
  if (h == 0) {
    cout << -1 << endl;
    return 0;
  }

  sort(e.begin(), e.end(), comp);
  for (int i = 0; sum > 0; i++) {
    int limit = (h - damage) / sum;
    if (e[i].h > limit) {
      h = 0;
      break;
    }
    damage += sum * (e[i].h - 1);
    sum -= e[i].a;
    damage += sum;
  }
  if (h <= damage) {
    cout << -1 << endl;
  } else {
    cout << damage << endl;
  }
  return 0;
}