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

Buggy submission (status: Memory Limit Exceeded):
#include <cstdio>
#include <cstring>
int m, n, ans;
int dp[2][1 << 21][2], fill, all = 1;
char c;
int main() {
  scanf("%d%d", &m, &n);
  fill = (1 << (n - 1)) - 1;
  dp[1][0][0] = 1;
  for (int i = 0; i < m * n; i++) {
    scanf(" %c", &c);
    if (c == '?') {
      all *= 3;
      all %= 100000;
    }
    memset(dp[i % 2], 0, sizeof(dp[i % 2]));
    for (int j = 0; j <= fill; j++) {
      int next = (j << 1) & fill;
      if (c == 'J' || c == '?') {
        dp[i % 2][next][1] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
      }
      if (c == 'O' || c == '?') {
        dp[i % 2][next][0] += dp[(i + 1) % 2][j][0];
        dp[i % 2][next + 1][0] += dp[(i + 1) % 2][j][1];
      }
      if (c == 'I' || c == '?') {
        if (j >> (n - 2) == 0 || i % n == n - 1) {
          dp[i % 2][next][0] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
        }
      }
      dp[i % 2][next][0] %= 100000;
      dp[i % 2][next][1] %= 100000;
      dp[i % 2][next + 1][0] %= 100000;
      dp[i % 2][next + 1][1] %= 100000;
    }
  }
  for (int i = 0; i <= fill; i++) {
    ans += dp[(m * n - 1) % 2][i][0] + dp[(m * n - 1) % 2][i][1];
    ans %= 100000;
  }
  printf("%d\n", (100000 + all - ans) % 100000);
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
int m, n, ans;
int dp[2][1 << 19][2], fill, all = 1;
char c;
int main() {
  scanf("%d%d", &m, &n);
  fill = (1 << (n - 1)) - 1;
  dp[1][0][0] = 1;
  for (int i = 0; i < m * n; i++) {
    scanf(" %c", &c);
    if (c == '?') {
      all *= 3;
      all %= 100000;
    }
    memset(dp[i % 2], 0, sizeof(dp[i % 2]));
    for (int j = 0; j <= fill; j++) {
      int next = (j << 1) & fill;
      if (c == 'J' || c == '?') {
        dp[i % 2][next][1] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
      }
      if (c == 'O' || c == '?') {
        dp[i % 2][next][0] += dp[(i + 1) % 2][j][0];
        dp[i % 2][next + 1][0] += dp[(i + 1) % 2][j][1];
      }
      if (c == 'I' || c == '?') {
        if (j >> (n - 2) == 0 || i % n == n - 1) {
          dp[i % 2][next][0] += dp[(i + 1) % 2][j][0] + dp[(i + 1) % 2][j][1];
        }
      }
      dp[i % 2][next][0] %= 100000;
      dp[i % 2][next][1] %= 100000;
      dp[i % 2][next + 1][0] %= 100000;
      dp[i % 2][next + 1][1] %= 100000;
    }
  }
  for (int i = 0; i <= fill; i++) {
    ans += dp[(m * n - 1) % 2][i][0] + dp[(m * n - 1) % 2][i][1];
    ans %= 100000;
  }
  printf("%d\n", (100000 + all - ans) % 100000);
  return 0;
}
Problem p00482 (C++)

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

using namespace std;

int main() {
  int M, N;
  cin >> M >> N;
  char g[20][21];
  for (int i = 0; i < M; i++) {
    cin >> g[i];
  }
  static int dp[2][2][1 << 20];
  dp[0][0][0] = 1;
  int s = M * N;
  int MOD = 100000;
  int all = 1;
  for (int i = 0; i < s; i++) {
    int y = i / N;
    int x = i % N;
    bool q = g[y][x] == '?';
    all = (all * (q ? 3 : 1)) % MOD;
    int bx = i % 2;
    int dx = bx ^ 1;
    fill(dp[dx][0], dp[dx + 1][0], 0);
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 1 << 20; l++) {
        int db = l & ~(1 << x);
        if (q || g[y][x] == 'J') {
          dp[dx][1][db] = (dp[dx][1][db] + dp[bx][k][l]) % MOD;
        }
        if (q || g[y][x] == 'O') {
          dp[dx][0][db | k << x] =
              (dp[dx][0][db | k << x] + dp[bx][k][l]) % MOD;
        }
        if (!(l >> x + 1 & 1) && (q || g[y][x] == 'I')) {
          dp[dx][0][db] = (dp[dx][0][db] + dp[bx][k][l]) % MOD;
        }
      }
    }
    if (x == s % N - 1) {
      for (int k = 0; k < 1 << 20; k++) {
        dp[dx][0][k] += dp[dx][1][k];
        dp[dx][1][k] = 0;
      }
    }
  }
  int a = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 1 << 20; j++) {
      a = (a + dp[s % 2][i][j]) % MOD;
    }
  }
  cout << (all - a + MOD) % MOD << endl;
}

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

using namespace std;

int main() {
  int M, N;
  cin >> M >> N;
  char g[20][21];
  for (int i = 0; i < M; i++) {
    cin >> g[i];
  }
  static int dp[2][2][1 << 20];
  dp[0][0][0] = 1;
  int s = M * N;
  int MOD = 100000;
  int all = 1;
  for (int i = 0; i < s; i++) {
    int y = i / N;
    int x = i % N;
    bool q = g[y][x] == '?';
    all = (all * (q ? 3 : 1)) % MOD;
    int bx = i % 2;
    int dx = bx ^ 1;
    fill(dp[dx][0], dp[dx + 1][0], 0);
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 1 << 20; l++) {
        if (!dp[bx][k][l])
          continue;
        int db = l & ~(1 << x);
        if (q || g[y][x] == 'J') {
          dp[dx][1][db] = (dp[dx][1][db] + dp[bx][k][l]) % MOD;
        }
        if (q || g[y][x] == 'O') {
          dp[dx][0][db | k << x] =
              (dp[dx][0][db | k << x] + dp[bx][k][l]) % MOD;
        }
        if (!(l >> x + 1 & 1) && (q || g[y][x] == 'I')) {
          dp[dx][0][db] = (dp[dx][0][db] + dp[bx][k][l]) % MOD;
        }
      }
    }
    if (x == s % N - 1) {
      for (int k = 0; k < 1 << 20; k++) {
        dp[dx][0][k] += dp[dx][1][k];
        dp[dx][1][k] = 0;
      }
    }
  }
  int a = 0;
  for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 1 << 20; j++) {
      a = (a + dp[s % 2][i][j]) % MOD;
    }
  }
  cout << (all - a + MOD) % MOD << endl;
}
Problem p00482 (C++)

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

typedef long long lli;
const int MAXH = 21;
const int MAXW = 21;
const lli mod = 100000LL;
const string C = "JOI";

int H, W;
char G[MAXH][MAXW];
map<int, lli> dp[2];

int main() {
  lli all = 1;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
      if (G[i][j] == '?') {
        all = (all * 3) % mod;
      }
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      bool p = (i * W + j) % 2;
      dp[!p].clear();
      for (map<int, lli>::iterator it = dp[p].begin(); it != dp[p].end();
           ++it) {
        int b = it->first;
        for (int k = 0; k < 3; ++k) {
          if (G[i][j] == '?' || G[i][j] == C[k]) {
            int nb = b & ~(1 << j);
            if (C[k] == 'J') {
              nb &= ~(1 << (j - 1));
              if (j + 1 != W)
                nb |= (1 << j);
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'O') {
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'I') {
              if (!(b & (1 << j))) {
                nb &= ~(1 << (j - 1));
                dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
              }
            }
          }
        }
      }
    }
  }
  lli sum = 0;
  for (int b = 0; b < (1 << W); ++b) {
    sum = (sum + dp[H * W % 2][b]) % mod;
  }
  cout << (all - sum + mod) % mod << endl;
  return 0;
}

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

typedef long long lli;
const int MAXH = 21;
const int MAXW = 21;
const lli mod = 100000LL;
const string C = "JOI";

int H, W;
char G[MAXH][MAXW];
map<int, lli> dp[2];

int main() {
  lli all = 1;
  cin >> H >> W;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      cin >> G[i][j];
      if (G[i][j] == '?') {
        all = (all * 3) % mod;
      }
    }
  }
  dp[0][0] = 1;
  for (int i = 0; i < H; ++i) {
    for (int j = 0; j < W; ++j) {
      bool p = (i * W + j) % 2;
      dp[!p].clear();
      for (map<int, lli>::iterator it = dp[p].begin(); it != dp[p].end();
           ++it) {
        int b = it->first;
        for (int k = 0; k < 3; ++k) {
          if (G[i][j] == '?' || G[i][j] == C[k]) {
            int nb = b & ~(1 << j);
            if (C[k] == 'J') {
              nb &= ~(1 << (j - 1));
              if (j + 1 != W)
                nb |= (1 << j);
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'O') {
              dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
            } else if (C[k] == 'I') {
              if (!(b & (1 << j))) {
                nb &= ~(1 << (j - 1));
                dp[!p][nb] = (dp[!p][nb] + dp[p][b]) % mod;
              }
            }
          }
        }
      }
    }
  }
  lli sum = 0;
  for (map<int, lli>::iterator it = dp[H * W % 2].begin();
       it != dp[H * W % 2].end(); ++it) {
    sum = (sum + it->second) % mod;
  }
  cout << (all - sum + mod) % mod << endl;
  return 0;
}
Problem p00483 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
static const LL INF = 1LL << 60LL;
typedef pair<int, int> pii;

string Map[1010];

LL J[1010][1010], O[1010][1010], I[1010][1010];
LL m, n, k;
LL a, b, c, d;
LL res_J, res_O, res_I;

int main() {
  cin >> m >> n;
  cin >> k;
  for (int y = 1; y <= m; ++y) {
    for (int x = 1; x <= n; ++x) {
      cin >> Map[y][x];
      if (Map[y][x] == 'J') {
        J[y][x]++;
      }
      if (Map[y][x] == 'O') {
        O[y][x]++;
      }
      if (Map[y][x] == 'I') {
        I[y][x]++;
      }
      J[y][x] += J[y][x - 1];
      O[y][x] += O[y][x - 1];
      I[y][x] += I[y][x - 1];
    }
  }
  for (int i = 0; i < k; ++i) {
    cin >> a >> b >> c >> d;
    int Min = min(a, c);
    int Max = max(a, c);
    for (int y = Min; y <= Max; ++y) {
      res_J += J[y][d] - J[y][b - 1];
      res_O += O[y][d] - O[y][b - 1];
      res_I += I[y][d] - I[y][b - 1];
    }
    cout << res_J << " " << res_O << " " << res_I << endl;
    res_J = 0;
    res_O = 0;
    res_I = 0;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
static const LL INF = 1LL << 60LL;
typedef pair<int, int> pii;

char Map[1010][1010];

LL J[1010][1010], O[1010][1010], I[1010][1010];
LL m, n, k;
LL a, b, c, d;
LL res_J, res_O, res_I;

int main() {
  cin >> m >> n;
  cin >> k;
  for (int y = 1; y <= m; ++y) {
    for (int x = 1; x <= n; ++x) {
      cin >> Map[y][x];
      if (Map[y][x] == 'J') {
        J[y][x]++;
      }
      if (Map[y][x] == 'O') {
        O[y][x]++;
      }
      if (Map[y][x] == 'I') {
        I[y][x]++;
      }
      J[y][x] += J[y][x - 1];
      O[y][x] += O[y][x - 1];
      I[y][x] += I[y][x - 1];
    }
  }
  for (int i = 0; i < k; ++i) {
    cin >> a >> b >> c >> d;
    int Min = min(a, c);
    int Max = max(a, c);
    for (int y = Min; y <= Max; ++y) {
      res_J += J[y][d] - J[y][b - 1];
      res_O += O[y][d] - O[y][b - 1];
      res_I += I[y][d] - I[y][b - 1];
    }
    cout << res_J << " " << res_O << " " << res_I << endl;
    res_J = 0;
    res_O = 0;
    res_I = 0;
  }
  return 0;
}
Problem p00483 (C++)

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

int joi2num(char c) {
  if (c == 'J')
    return 0;
  if (c == 'O')
    return 1;
  return 2;
}

int main() {
  int m, n, k;
  char fld[1024][1024];
  int sum[1024][1024][3];
  scanf("%d%d%d", &m, &n, &k);
  for (int x = 0; x < m; ++x)
    scanf("%s", fld[x]);
  for (int i = 0; i < 3; ++i)
    sum[0][0][i] = (i == joi2num(fld[0][0]));
  for (int x = 1; x < m; ++x) {
    for (int i = 0; i < 3; ++i)
      sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
  }
  for (int y = 1; y < n; ++y) {
    for (int i = 0; i < 3; ++i)
      sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
  }
  for (int x = 1; x < m; ++x) {
    for (int y = 1; y < n; ++y) {
      for (int i = 0; i < 3; ++i) {
        int temp = 0;
        temp += sum[x - 1][y][i];
        temp += sum[x][y - 1][i];
        temp -= sum[x - 1][y - 1][i];
        sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    int ax, ay, bx, by;
    scanf("%d%d%d%d", &ax, &ay, &bx, &by);
    ax -= 1, ay -= 1, bx -= 1, by -= 1;
    int ans[3] = {0};
    for (int j = 0; j < 3; ++j) {
      ans[j] += sum[bx][by][j];
      ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
      ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
      ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
    }
    printf("%d %d %d\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}

Fixed submission:
#include <cstdio>

int joi2num(char c) {
  if (c == 'J')
    return 0;
  if (c == 'O')
    return 1;
  return 2;
}

int main() {
  int m, n, k;
  static char fld[1024][1024];
  static int sum[1024][1024][3] = {0};
  scanf("%d%d%d", &m, &n, &k);
  for (int x = 0; x < m; ++x)
    scanf("%s", fld[x]);
  for (int i = 0; i < 3; ++i)
    sum[0][0][i] = (i == joi2num(fld[0][0]));
  for (int x = 1; x < m; ++x) {
    for (int i = 0; i < 3; ++i)
      sum[x][0][i] = sum[x - 1][0][i] + (i == joi2num(fld[x][0]));
  }
  for (int y = 1; y < n; ++y) {
    for (int i = 0; i < 3; ++i)
      sum[0][y][i] = sum[0][y - 1][i] + (i == joi2num(fld[0][y]));
  }
  for (int x = 1; x < m; ++x) {
    for (int y = 1; y < n; ++y) {
      for (int i = 0; i < 3; ++i) {
        int temp = 0;
        temp += sum[x - 1][y][i];
        temp += sum[x][y - 1][i];
        temp -= sum[x - 1][y - 1][i];
        sum[x][y][i] = temp + (i == joi2num(fld[x][y]));
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    int ax, ay, bx, by;
    scanf("%d%d%d%d", &ax, &ay, &bx, &by);
    ax -= 1, ay -= 1, bx -= 1, by -= 1;
    int ans[3] = {0};
    for (int j = 0; j < 3; ++j) {
      ans[j] += sum[bx][by][j];
      ans[j] -= ax > 0 ? sum[ax - 1][by][j] : 0;
      ans[j] -= ay > 0 ? sum[bx][ay - 1][j] : 0;
      ans[j] += ax > 0 && ay > 0 ? sum[ax - 1][ay - 1][j] : 0;
    }
    printf("%d %d %d\n", ans[0], ans[1], ans[2]);
  }
  return 0;
}
Problem p00484 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long
int dp[11][2001];
int book[11][2001];
int kind[11] = {0};
int acc[11][2001];
int main() {
  int N, K;
  int c, g;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> c >> g;
    book[g - 1][kind[g - 1]++] = c;
  }
  for (int i = 0; i < 11; i++) {
    sort(book[i], book[i] + kind[i]);
    reverse(book[i], book[i] + kind[i]);
  }
  for (int i = 0; i < 11; i++) {
    acc[i][0] = 0;
    for (int j = 1; j <= kind[i]; j++) {
      acc[i][j] = acc[i][j - 1] + book[i][j - 1] + 2 * (j - 1);
    }
  }
  for (int i = 0; i <= K; i++) {
    if (0 < i && i <= kind[0])
      dp[0][i] = acc[0][i];
    else
      dp[0][i] = 0;
  }

  for (int i = 1; i <= 11; i++) {
    for (int j = 0; j <= K; j++) {
      dp[i][j] = 0;
      for (int k = 0; k <= min(j, kind[i]); k++) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + acc[i][k]);
      }
    }
  }
  cout << dp[11][K] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long
int dp[12][2001];
int book[11][2001];
int kind[11] = {0};
int acc[11][2001];
int main() {
  int N, K;
  int c, g;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> c >> g;
    book[g - 1][kind[g - 1]++] = c;
  }
  for (int i = 0; i < 11; i++) {
    sort(book[i], book[i] + kind[i]);
    reverse(book[i], book[i] + kind[i]);
  }
  for (int i = 0; i < 11; i++) {
    acc[i][0] = 0;
    for (int j = 1; j <= kind[i]; j++) {
      acc[i][j] = acc[i][j - 1] + book[i][j - 1] + 2 * (j - 1);
    }
  }
  for (int i = 0; i <= K; i++) {
    if (0 < i && i <= kind[0])
      dp[0][i] = acc[0][i];
    else
      dp[0][i] = 0;
  }

  for (int i = 1; i <= 11; i++) {
    for (int j = 0; j <= K; j++) {
      dp[i][j] = 0;
      for (int k = 0; k <= min(j, kind[i]); k++) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + acc[i][k]);
      }
    }
  }
  cout << dp[11][K] << endl;
  return 0;
}
Problem p00484 (C++)

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

vector<int> cost[10];
int dp[10][2001];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, n) {
    int c, g;
    scanf("%d%d", &c, &g);
    cost[--g].push_back(c);
  }
  rep(i, 10) sort(cost[i].begin(), cost[i].end(), greater<int>());
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i][j] = dp[i - 1][j];
      int sum = 0;
      for (int t = 0; t < min(j, (int)cost[i - 1].size()); t++) {
        sum += cost[i - 1][t];
        dp[i][j] = max(dp[i][j], dp[i - 1][j - t - 1] + sum + t * (t + 1));
      }
    }
  }
  printf("%d\n", dp[10][k]);
}

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

vector<int> cost[10];
int dp[15][2001];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, n) {
    int c, g;
    scanf("%d%d", &c, &g);
    cost[--g].push_back(c);
  }
  rep(i, 10) sort(cost[i].begin(), cost[i].end(), greater<int>());
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i][j] = dp[i - 1][j];
      int sum = 0;
      for (int t = 0; t < min(j, (int)cost[i - 1].size()); t++) {
        sum += cost[i - 1][t];
        dp[i][j] = max(dp[i][j], dp[i - 1][j - t - 1] + sum + t * (t + 1));
      }
    }
  }
  printf("%d\n", dp[10][k]);
}
Problem p00484 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 2005;

vector<int> a[10];
vector<int> b[10];
int dp[10][MAX_N];
int n, K;

int main() {
  int x, y;
  scanf("%d%d", &n, &K);
  rep(i, n) {
    scanf("%d%d", &x, &y);
    a[y - 1].push_back(x);
  }
  rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
  rep(i, 10) {
    int suma = 0;
    rep(j, a[i].size()) {
      suma += a[i][j] + 2 * j;
      b[i].push_back(suma);
    }
  }
  rep(j, K + 1) { dp[0][j] = 0; }
  rep(i, 10) {
    rep(j, K + 1) {
      dp[i + 1][j] = dp[i][j];
      for (int k = 1; k <= min((int)b[i].size(), j); k++) {
        dp[i + 1][j] =
            max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
      }
    }
  }
  printf("%d\n", dp[10][K]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 2005;

vector<int> a[11];
vector<int> b[11];
int dp[11][MAX_N];
int n, K;

int main() {
  int x, y;
  scanf("%d%d", &n, &K);
  rep(i, n) {
    scanf("%d%d", &x, &y);
    a[y - 1].push_back(x);
  }
  rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
  rep(i, 10) {
    int suma = 0;
    rep(j, a[i].size()) {
      suma += a[i][j] + 2 * j;
      b[i].push_back(suma);
    }
  }
  rep(j, K + 1) { dp[0][j] = 0; }
  rep(i, 10) {
    rep(j, K + 1) {
      dp[i + 1][j] = dp[i][j];
      for (int k = 1; k <= min((int)b[i].size(), j); k++) {
        dp[i + 1][j] =
            max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
      }
    }
  }
  printf("%d\n", dp[10][K]);
  return 0;
}
Problem p00484 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  long long int lis[11][2003], lissize[11] = {0};
  for (int i = 0; i < 11; i++) {
    for (int j = 1; j < 2003; j++)
      lis[i][j] = -2000000000;
  }
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    lis[b][lissize[b] + 1] = a;
    lissize[b]++;
  }
  for (int i = 0; i < 11; i++) {
    sort(lis[i] + 1, lis[i] + 2003);
    reverse(lis[i] + 1, lis[i] + 2003);
  }
  for (int i = 0; i < 11; i++) {
    for (int j = 1; j < 2003; j++)
      lis[i][j] += lis[i][j - 1] + (j - 1) * 2;
  }
  /*
  for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
          if(0<=lis[i][j])printf("[%lld]",lis[i][j]);
      }
      puts("");
  }
  */
  long long int fie[12][4003] = {0};
  for (int i = 0; i < 11; i++) {
    for (int j = 0; j < 2001; j++) {
      for (int k = 0; k < 2001; k++) {
        if (j + k <= 2000)
          fie[i + 1][j + k] = max(fie[i + 1][j + k], fie[i][j] + lis[i + 1][k]);
      }
    }
  }
  cout << fie[10][K] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  long long int lis[11][2003], lissize[11] = {0};
  for (int i = 0; i < 11; i++) {
    for (int j = 1; j < 2003; j++)
      lis[i][j] = -2000000000;
  }
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    lis[b][lissize[b] + 1] = a;
    lissize[b]++;
  }
  for (int i = 0; i < 11; i++) {
    sort(lis[i] + 1, lis[i] + 2003);
    reverse(lis[i] + 1, lis[i] + 2003);
  }
  for (int i = 0; i < 11; i++) {
    for (int j = 1; j < 2003; j++)
      lis[i][j] += lis[i][j - 1] + (j - 1) * 2;
  }
  /*
  for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
          if(0<=lis[i][j])printf("[%lld]",lis[i][j]);
      }
      puts("");
  }
  */
  long long int fie[13][4003] = {0};
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 2001; j++) {
      for (int k = 0; k < 2001; k++) {
        if (j + k <= 2000)
          fie[i + 1][j + k] = max(fie[i + 1][j + k], fie[i][j] + lis[i + 1][k]);
      }
    }
  }
  cout << fie[10][K] << endl;
  return 0;
}
Problem p00484 (C++)

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

using namespace std;

int main() {
  for (int n, k; cin >> n >> k, n | k;) {
    vector<vector<int>> ps(10);
    for (int i = 0; i < n; i++) {
      int p, g;
      cin >> p >> g;
      ps[g - 1].push_back(p);
    }

    for (int i = 0; i < 10; i++)
      sort(ps[i].begin(), ps[i].end(), greater<int>());
    vector<vector<int>> ss = ps;
    for (int i = 0; i < 10; i++)
      for (int j = 1; j < ss[i].size(); j++)
        ss[i][j] += ss[i][j - 1] + 2 * j;

    vector<vector<int>> dp(2, vector<int>(n + 1));
    for (int i = 0; i < 10; i++) {
      dp[i + 1 & 1] = dp[i & 1];
      for (int j = 0; j < ss[i].size(); j++)
        for (int k = n; k > j; k--)
          dp[i + 1 & 1][k] =
              max(dp[i + 1 & 1][k], dp[i & 1][k - (j + 1)] + ss[i][j]);
    }
    cout << dp[0][k] << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
  for (int n, k; cin >> n >> k && n | k;) {
    vector<vector<int>> ps(10);
    for (int i = 0; i < n; i++) {
      int p, g;
      cin >> p >> g;
      ps[g - 1].push_back(p);
    }

    for (int i = 0; i < 10; i++)
      sort(ps[i].begin(), ps[i].end(), greater<int>());
    vector<vector<int>> ss = ps;
    for (int i = 0; i < 10; i++)
      for (int j = 1; j < ss[i].size(); j++)
        ss[i][j] += ss[i][j - 1] + 2 * j;

    vector<vector<int>> dp(2, vector<int>(n + 1));
    for (int i = 0; i < 10; i++) {
      dp[i + 1 & 1] = dp[i & 1];
      for (int j = 0; j < ss[i].size(); j++)
        for (int k = n; k > j; k--)
          dp[i + 1 & 1][k] =
              max(dp[i + 1 & 1][k], dp[i & 1][k - (j + 1)] + ss[i][j]);
    }
    cout << dp[0][k] << endl;
  }
  return 0;
}
Problem p00484 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> pr(10, vector<int>(n + 1, 0));
  vector<int> ko(10, 0);
  for (int i = 0; i < n; i++) {
    int c, g;
    cin >> c >> g;
    g--;
    pr[g][ko[g]] = c;
    ko[g]++;
  }
  for (int i = 0; i < 10; i++) {
    sort(pr[i].begin(), pr[i].end());
    reverse(pr[i].begin(), pr[i].end());
  }
  vector<vector<int>> sum(10, vector<int>(n + 1, 0));
  for (int i = 0; i < n; i++) {
    int su = 0;
    for (int j = 1; j <= ko[i]; j++) {
      su += pr[i][j - 1];
      sum[i][j] = su + j * (j - 1);
    }
  }
  vector<vector<int>> dp(10, vector<int>(n + 1, 0));
  dp[0] = sum[0];
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j <= k; j++) {
      for (int kk = j; kk <= k; kk++) {
        dp[i][kk] = max(dp[i][kk], dp[i - 1][j] + sum[i][kk - j]);
      }
    }
  }
  cout << dp[9][k] << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  vector<vector<int>> pr(10, vector<int>(n + 1, 0));
  vector<int> ko(10, 0);
  for (int i = 0; i < n; i++) {
    int c, g;
    cin >> c >> g;
    g--;
    pr[g][ko[g]] = c;
    ko[g]++;
  }
  for (int i = 0; i < 10; i++) {
    sort(pr[i].begin(), pr[i].end());
    reverse(pr[i].begin(), pr[i].end());
  }
  vector<vector<int>> sum(10, vector<int>(n + 1, 0));
  for (int i = 0; i < 10; i++) {
    int su = 0;
    for (int j = 1; j <= ko[i]; j++) {
      su += pr[i][j - 1];
      sum[i][j] = su + j * (j - 1);
    }
  }
  vector<vector<int>> dp(10, vector<int>(n + 1, 0));
  dp[0] = sum[0];
  for (int i = 1; i < 10; i++) {
    for (int j = 0; j <= k; j++) {
      for (int kk = j; kk <= k; kk++) {
        dp[i][kk] = max(dp[i][kk], dp[i - 1][j] + sum[i][kk - j]);
      }
    }
  }
  cout << dp[9][k] << endl;
  return 0;
}
Problem p00484 (C++)

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

using namespace std;

long long int cost[10][3000];
long long int DP[3000];

int main() {

  int N, K;

  while (true) {
    cin >> N >> K;
    if (cin.eof()) {
      break;
    }

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 3000; j++) {
        cost[i][j] = 0;
      }
    }

    vector<int> v_cost[10];

    for (int i = 0; i < N; i++) {
      int c, g;
      cin >> c >> g;
      v_cost[g].push_back(c);
    }

    for (int i = 0; i < 10; i++) {
      sort(v_cost[i].begin(), v_cost[i].end());
      long long int sum = 0;
      for (int j = v_cost[i].size() - 1; j >= 0; j--) {
        int count = v_cost[i].size() - j - 1;
        sum += v_cost[i][j];
        cost[i][count] = sum + count * (count + 1);
      }
    }

    for (int i = 0; i < 3000; i++) {
      DP[i] = 0;
    }

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j <= K; j++) {
        for (int k = 0; k < K; k++) {
          if (j + k + 1 > K) {
            break;
          }
          DP[j] = max(DP[j], DP[j + k + 1] + cost[i][k]);
        }
        // cout << DP[j] << " ";
      }
      // cout << endl;
    }

    cout << DP[0] << endl;
  }

  return 0;
}

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

using namespace std;

long long int cost[10][3000];
long long int DP[3000];

int main() {

  int N, K;

  while (true) {
    cin >> N >> K;
    if (cin.eof()) {
      break;
    }

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j < 3000; j++) {
        cost[i][j] = 0;
      }
    }

    vector<int> v_cost[10];

    for (int i = 0; i < N; i++) {
      int c, g;
      cin >> c >> g;
      g--;
      v_cost[g].push_back(c);
    }

    for (int i = 0; i < 10; i++) {
      sort(v_cost[i].begin(), v_cost[i].end());
      long long int sum = 0;
      for (int j = v_cost[i].size() - 1; j >= 0; j--) {
        int count = v_cost[i].size() - j - 1;
        sum += v_cost[i][j];
        cost[i][count] = sum + count * (count + 1);
      }
    }

    for (int i = 0; i < 3000; i++) {
      DP[i] = 0;
    }

    for (int i = 0; i < 10; i++) {
      for (int j = 0; j <= K; j++) {
        for (int k = 0; k < K; k++) {
          if (j + k + 1 > K) {
            break;
          }
          DP[j] = max(DP[j], DP[j + k + 1] + cost[i][k]);
        }
        // cout << DP[j] << " ";
      }
      // cout << endl;
    }

    cout << DP[0] << endl;
  }

  return 0;
}
Problem p00484 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, K, latte[2001] = {0};
  for (int i = 1; i <= 2000; i++)
    latte[i] = (i - 1) * i;

  cin >> N >> K;
  vector<vector<int>> books(10);
  for (int i = 0; i < N; i++) {
    int v, j;
    cin >> v >> j;
    j--;
    books[j].push_back(v);
  }

  for (int i = 0; i < 10; i++) {
    sort(books[i].begin(), books[i].end(), greater<int>());
    for (int j = 0; j < books[i].size(); j++) {
      if (j + 1 == books[i].size()) {
        books[i][j] += j * (j + 1);
        continue;
      }
      books[i][j + 1] += books[i][j];
      books[i][j] += j * (j + 1);
    }
  }

  int dp[11][2001];
  fill(dp[0], dp[0] + 11 * 2001, -1);
  for (int i = 0; i <= N; i++)
    dp[i][0] = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= K; j++) {
      if (dp[i][j] == -1)
        break;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      for (int k = 0; k < books[i].size(); k++) {
        if (j + k + 1 > K)
          break;
        dp[i + 1][j + k + 1] =
            max(dp[i + 1][j + k + 1], dp[i][j] + books[i][k]);
      }
    }
  }
  /*
  for(int i=0;i<10;i++){
      if(!books[i].size())continue;
      cout<<i<<":";
      for(int j=0;j<books[i].size();j++)cout<<books[i][j]<<" ";
      cout<<endl;
  }
  cout<<endl<<endl;
  for(int i=0;i<=10;i++){
      for(int j=0;j<=K;j++)cout<<dp[i][j]<<" ";
      cout<<endl;
  }
  */
  cout << dp[10][K] << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, K, latte[2001] = {0};
  for (int i = 1; i <= 2000; i++)
    latte[i] = (i - 1) * i;

  cin >> N >> K;
  vector<vector<int>> books(10);
  for (int i = 0; i < N; i++) {
    int v, j;
    cin >> v >> j;
    j--;
    books[j].push_back(v);
  }

  for (int i = 0; i < 10; i++) {
    sort(books[i].begin(), books[i].end(), greater<int>());
    for (int j = 0; j < books[i].size(); j++) {
      if (j + 1 == books[i].size()) {
        books[i][j] += j * (j + 1);
        continue;
      }
      books[i][j + 1] += books[i][j];
      books[i][j] += j * (j + 1);
    }
  }

  int dp[11][2001];
  fill(dp[0], dp[0] + 11 * 2001, -1);
  for (int i = 0; i <= 10; i++)
    dp[i][0] = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= K; j++) {
      if (dp[i][j] == -1)
        break;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      for (int k = 0; k < books[i].size(); k++) {
        if (j + k + 1 > K)
          break;
        dp[i + 1][j + k + 1] =
            max(dp[i + 1][j + k + 1], dp[i][j] + books[i][k]);
      }
    }
  }
  /*
  for(int i=0;i<10;i++){
      if(!books[i].size())continue;
      cout<<i<<":";
      for(int j=0;j<books[i].size();j++)cout<<books[i][j]<<" ";
      cout<<endl;
  }
  cout<<endl<<endl;
  for(int i=0;i<=10;i++){
      for(int j=0;j<=K;j++)cout<<dp[i][j]<<" ";
      cout<<endl;
  }
  */
  cout << dp[10][K] << endl;
}
Problem p00485 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

struct TOWN {
  bool kaku;
  int kyo;
  vector<pair<int, int>> road;

  TOWN() : kaku(false) {}
} town[3001];

int main() {
  int n, m, k, a, b, l, ina[3000], inb[3000], inl[3000], ans = 0;
  priority_queue<pair<int, int>> pq;
  pair<int, int> p;

  scanf("%d%d%d", &n, &m, &k);

  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &l);
    town[a].road.push_back(make_pair(b, -l));
    town[b].road.push_back(make_pair(a, -l));
    ina[i] = a;
    inb[i] = b;
    inl[i] = l;
  }

  for (int i = 0; i < k; i++) {
    scanf("%d", &a);
    pq.push(make_pair(0, a));
  }

  while (!pq.empty()) {
    p = pq.top();
    pq.pop();

    if (town[p.second].kaku)
      continue;
    town[p.second].kaku = true;
    town[p.second].kyo = -p.first;

    for (int i = 0; i < town[p.second].road.size(); i++)
      pq.push(make_pair(p.first + town[p.second].road[i].second,
                        town[p.second].road[i].first));
  }

  for (int i = 0; i < m; i++) {
    int sa = abs(town[ina[i]].kyo - town[inb[i]].kyo);
    ans = max(ans, (max(inl[i] - sa, 0) + 1) / 2 +
                       max(town[ina[i]].kyo, town[inb[i]].kyo));
  }

  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;

struct TOWN {
  bool kaku;
  int kyo;
  vector<pair<int, int>> road;

  TOWN() : kaku(false) {}
} town[3001];

int main() {
  int n, m, k, a, b, l, ina[100000], inb[100000], inl[100000], ans = 0;
  priority_queue<pair<int, int>> pq;
  pair<int, int> p;

  scanf("%d%d%d", &n, &m, &k);

  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &l);
    town[a].road.push_back(make_pair(b, -l));
    town[b].road.push_back(make_pair(a, -l));
    ina[i] = a;
    inb[i] = b;
    inl[i] = l;
  }

  for (int i = 0; i < k; i++) {
    scanf("%d", &a);
    pq.push(make_pair(0, a));
  }

  while (!pq.empty()) {
    p = pq.top();
    pq.pop();

    if (town[p.second].kaku)
      continue;
    town[p.second].kaku = true;
    town[p.second].kyo = -p.first;

    for (int i = 0; i < town[p.second].road.size(); i++)
      pq.push(make_pair(p.first + town[p.second].road[i].second,
                        town[p.second].road[i].first));
  }

  for (int i = 0; i < m; i++) {
    int sa = abs(town[ina[i]].kyo - town[inb[i]].kyo);
    ans = max(ans, (max(inl[i] - sa, 0) + 1) / 2 +
                       max(town[ina[i]].kyo, town[inb[i]].kyo));
  }

  printf("%d\n", ans);

  return 0;
}
Problem p00485 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int N, M, K, A, B, L;

vector<vector<pair<int, int>>> G;

vector<int> P, D;

int main() {
  scanf("%d", &N);
  scanf("%d", &M);
  scanf("%d", &K);

  G = vector<vector<pair<int, int>>>(N);

  for (int i = 0; i < M; i++) {
    scanf("%d", &A);
    scanf("%d", &B);
    scanf("%d", &L);

    G[A - 1].push_back(make_pair(B - 1, L));
    G[B - 1].push_back(make_pair(A - 1, L));
  }

  P = vector<int>(K);

  for (int i = 0; i < K; i++) {
    scanf("%d", &P[i]);
    P[i]--; // 0 - indexed
  }

  D = vector<int>(N, 999999999);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;

  for (int i = 0; i < K; i++) {
    que.push(make_pair(0, P[i]));
    D[P[i]] = 0;
  }

  while (!que.empty()) {
    pair<int, int> state = que.top();
    que.pop();

    int dist = state.first;
    int node = state.second;

    for (int j = 0; j < G[node].size(); j++) {
      int node2 = G[node][j].first;
      int leng2 = G[node][j].second;

      if (D[node2] < D[node] + leng2) {
        D[node2] = D[node] + leng2;

        que.push(make_pair(D[node2], node2));
      }
    }
  }

  int ret = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      ret = max(ret, D[i] + D[G[i][j].first] + G[i][j].second);
    }
  }

  printf("%d\n", (ret + 1) / 2);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int N, M, K, A, B, L;

vector<vector<pair<int, int>>> G;

vector<int> P, D;

int main() {
  scanf("%d", &N);
  scanf("%d", &M);
  scanf("%d", &K);

  G = vector<vector<pair<int, int>>>(N);

  for (int i = 0; i < M; i++) {
    scanf("%d", &A);
    scanf("%d", &B);
    scanf("%d", &L);

    G[A - 1].push_back(make_pair(B - 1, L));
    G[B - 1].push_back(make_pair(A - 1, L));
  }

  P = vector<int>(K);

  for (int i = 0; i < K; i++) {
    scanf("%d", &P[i]);
    P[i]--; // 0 - indexed
  }

  D = vector<int>(N, 999999999);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;

  for (int i = 0; i < K; i++) {
    que.push(make_pair(0, P[i]));
    D[P[i]] = 0;
  }

  while (!que.empty()) {
    pair<int, int> state = que.top();
    que.pop();

    int dist = state.first;
    int node = state.second;

    for (int j = 0; j < G[node].size(); j++) {
      int node2 = G[node][j].first;
      int leng2 = G[node][j].second;

      if (D[node2] > D[node] + leng2) {
        D[node2] = D[node] + leng2;

        que.push(make_pair(D[node2], node2));
      }
    }
  }

  int ret = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < G[i].size(); j++) {
      ret = max(ret, D[i] + D[G[i][j].first] + G[i][j].second);
    }
  }

  printf("%d\n", (ret + 1) / 2);

  return 0;
}
Problem p00486 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int ox[100000];
int oy[100000];
int x[100000];
int y[100000];
int n;
long long calc(int a, int b) {
  int md = 0, i;
  long long ans = 0;
  for (i = 0; i < n; i++) {
    int d = abs(ox[i] - a) + abs(oy[i] - b);
    md = max(md, d);
    ans += (long long)d * 2;
  }
  return ans - (long long)md;
}
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int w, h, i, j;
  scanf("%d %d", &w, &h);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &ox[i], &oy[i]);
    x[i] = ox[i];
    y[i] = oy[i];
  }
  sort(x, x + n);
  sort(y, y + n);
  long long ans = 1000000000000000000LL;
  int ax[2];
  int ay[2];
  ax[0] = x[n / 2];
  ay[0] = y[n / 2];
  int nx, ny;
  if (n % 2 == 0) {
    ax[1] = x[n / 2 - 1];
    ay[1] = y[n / 2 - 1];
    for (i = 1; i >= 0; i++)
      for (j = 1; j >= 0; j++) {
        if (ans > calc(ax[i], ay[j])) {
          ans = calc(ax[i], ay[j]);
          nx = ax[i];
          ny = ay[j];
        }
      }
  } else {
    ans = calc(ax[0], ay[0]);
    nx = ax[0];
    ny = ay[0];
  }
  printf("%lld\n", ans);
  printf("%d %d\n", nx, ny);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int ox[100000];
int oy[100000];
int x[100000];
int y[100000];
int n;
long long calc(int a, int b) {
  int md = 0, i;
  long long ans = 0;
  for (i = 0; i < n; i++) {
    int d = abs(ox[i] - a) + abs(oy[i] - b);
    md = max(md, d);
    ans += (long long)d * 2;
  }
  return ans - (long long)md;
}
int max(int a, int b) { return a > b ? a : b; }
int main() {
  int w, h, i, j;
  scanf("%d %d", &w, &h);
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &ox[i], &oy[i]);
    x[i] = ox[i];
    y[i] = oy[i];
  }
  sort(x, x + n);
  sort(y, y + n);
  long long ans = 1000000000000000000LL;
  int ax[2];
  int ay[2];
  ax[0] = x[n / 2];
  ay[0] = y[n / 2];
  int nx, ny;
  if (n % 2 == 0) {
    ax[1] = x[n / 2 - 1];
    ay[1] = y[n / 2 - 1];
    for (i = 1; i >= 0; i--)
      for (j = 1; j >= 0; j--) {
        if (ans > calc(ax[i], ay[j])) {
          ans = calc(ax[i], ay[j]);
          nx = ax[i];
          ny = ay[j];
        }
      }
  } else {
    ans = calc(ax[0], ay[0]);
    nx = ax[0];
    ny = ay[0];
  }
  printf("%lld\n", ans);
  printf("%d %d\n", nx, ny);
  return 0;
}
Problem p00486 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;

int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];

ll calc(ll X, ll Y) {
  ll ma = 0LL;
  ll tot = 0LL;
  FOR(i, n) {
    ll d = labs(X - x[i]) + labs(Y - y[i]);
    ma = max(ma, d);
    tot += d;
  }
  return 2 * tot - ma;
}

void solve() {
  cin >> h >> w;
  cin >> n;
  FOR(i, n) cin >> x[i] >> y[i];
  FOR(i, n) cx[i] = x[i];
  FOR(i, n) cy[i] = y[i];
  sort(x, cx + n);
  sort(y, cy + n);
  ll mx[2], my[2];
  mx[0] = cx[(n - 1) >> 1];
  mx[1] = cx[n >> 1];
  my[0] = cy[(n - 1) >> 1];
  my[1] = cy[n >> 1];
  ll mi = LONG_MAX / 4, miX, miY;
  FOR(i, 2) {
    FOR(j, 2) {
      ll ret = calc(mx[i], my[j]);
      if (ret < mi) {
        mi = ret;
        miX = mx[i];
        miY = my[j];
      }
    }
  }
  cout << mi << endl;
  cout << miX << " " << miY << endl;
}

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

Fixed submission:
#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;

int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];

ll calc(ll X, ll Y) {
  ll ma = 0LL;
  ll tot = 0LL;
  FOR(i, n) {
    ll d = labs(X - x[i]) + labs(Y - y[i]);
    ma = max(ma, d);
    tot += d;
  }
  return 2 * tot - ma;
}

void solve() {
  cin >> h >> w;
  cin >> n;
  FOR(i, n) cin >> x[i] >> y[i];
  FOR(i, n) cx[i] = x[i];
  FOR(i, n) cy[i] = y[i];
  sort(cx, cx + n);
  sort(cy, cy + n);
  ll mx[2], my[2];
  mx[0] = cx[(n - 1) >> 1];
  mx[1] = cx[n >> 1];
  my[0] = cy[(n - 1) >> 1];
  my[1] = cy[n >> 1];
  ll mi = LONG_MAX / 4, miX, miY;
  FOR(i, 2) {
    FOR(j, 2) {
      ll ret = calc(mx[i], my[j]);
      if (ret < mi) {
        mi = ret;
        miX = mx[i];
        miY = my[j];
      }
    }
  }
  cout << mi << endl;
  cout << miX << " " << miY << endl;
}

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

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

int N, n;
pair<int, int> p[300000];
bool ok(int t) {
  priority_queue<int> Q;
  ll sum = 0;
  for (int i = n - 1; i >= n - t + 1; i--) {
    Q.push(p[i].second);
    sum += p[i].second;
  }
  if (p[n - t].first * (ll)t >= (ll)sum + p[n - t].second)
    return 1;
  for (int i = n - t; i > 0; i--) {
    if (Q.top() > p[i].second) {
      sum -= Q.top() - p[i].second;
      Q.pop();
      Q.push(p[i].second);
    }
    if (p[i - 1].first * (ll)t >= (ll)sum + p[i - 1].second)
      return 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  rep(i, n) {
    int a, b;
    scanf("%d%d", &a, &b);
    p[i] = {b, a};
  }
  sort(p, p + n);
  int a = 0, b = n;
  while (a != b) {
    int t = (a + b) / 2;
    if (b - a == 1)
      t = b;
    if (ok(t))
      a = t;
    else
      b = t - 1;
  }
  printf("%d\n", a);
}

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

int N, n;
pair<int, int> p[300000];
bool ok(int t) {
  priority_queue<int> Q;
  ll sum = 0;
  for (int i = n - 1; i >= n - t + 1; i--) {
    Q.push(p[i].second);
    sum += p[i].second;
  }
  if (p[n - t].first * (ll)t >= (ll)sum + p[n - t].second)
    return 1;
  for (int i = n - t; i > 0; i--) {
    if (!Q.empty() && Q.top() > p[i].second) {
      sum -= Q.top() - p[i].second;
      Q.pop();
      Q.push(p[i].second);
    }
    if (p[i - 1].first * (ll)t >= (ll)sum + p[i - 1].second)
      return 1;
  }
  return 0;
}
int main() {
  scanf("%d", &n);
  rep(i, n) {
    int a, b;
    scanf("%d%d", &a, &b);
    p[i] = {b, a};
  }
  sort(p, p + n);
  int a = 0, b = n;
  while (a != b) {
    int t = (a + b) / 2;
    if (b - a == 1)
      t = b;
    if (ok(t))
      a = t;
    else
      b = t - 1;
  }
  printf("%d\n", a);
}
Problem p00487 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define filein(t) freopen(t, "r", stdin)
#define fileout(t) freopen(t, "w", stdout)
#define f first
#define s second
#define cs 137
#define ll long long
#define mp make_pair
#define N 100025
#define ln 1e16
#define oo 111539786
#define Pa pair<int, int>
#define Pi pair<int, Pa>

using namespace std;

Pa a[N];
priority_queue<int> que;
int n;

bool Check(int X) {
  while (!que.empty())
    que.pop();
  ll S = 0;
  for (int i = n; i > n - X; i--) {
    que.push(a[i].s);
    S += a[i].s;
  }
  if (S <= (ll)X * a[n - X + 1].f)
    return true;
  for (int i = n - X; i >= 1; i--) {
    int Max = que.top();
    if (Max > a[i].s) {
      S = S - Max + a[i].s;
      que.pop();
      que.push(a[i].s);
    }
    if (S <= (ll)X * a[i].f)
      return true;
  }
  return false;
}

int main() {
  //    filein("MICROORGANISM.inp");    fileout("MICROORGANISM.out");
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].s >> a[i].f;
  sort(a + 1, a + n + 1);
  int L = 0;
  int R = n + 1;
  while (R - L > 1) {
    int X = (L + R) / 2;
    if (Check(X))
      L = X;
    else
      R = X;
  }
  cout << L << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define filein(t) freopen(t, "r", stdin)
#define fileout(t) freopen(t, "w", stdout)
#define f first
#define s second
#define cs 137
#define ll long long
#define mp make_pair
#define N 300025
#define ln 1e16
#define oo 111539786
#define Pa pair<int, int>
#define Pi pair<int, Pa>

using namespace std;

Pa a[N];
priority_queue<int> que;
int n;

bool Check(int X) {
  while (!que.empty())
    que.pop();
  ll S = 0;
  for (int i = n; i > n - X; i--) {
    que.push(a[i].s);
    S += a[i].s;
  }
  if (S <= (ll)X * a[n - X + 1].f)
    return true;
  for (int i = n - X; i >= 1; i--) {
    int Max = que.top();
    if (Max > a[i].s) {
      S = S - Max + a[i].s;
      que.pop();
      que.push(a[i].s);
    }
    if (S <= (ll)X * a[i].f)
      return true;
  }
  return false;
}

int main() {
  //    filein("MICROORGANISM.inp");    fileout("MICROORGANISM.out");
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i].s >> a[i].f;
  sort(a + 1, a + n + 1);
  int L = 0;
  int R = n + 1;
  while (R - L > 1) {
    int X = (L + R) / 2;
    if (Check(X))
      L = X;
    else
      R = X;
  }
  cout << L << endl;
}
Problem p00489 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
int A[101], B[101], C[101], D[101];
int N, M;
int S[101] = {};
int main() {
  cin >> N;
  for (int i = 0; i < (N * (N - 1) / 2); i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    if (C[i] > D[i])
      S[A[i]] += 3;
    if (C[i] < D[i])
      S[B[i]] += 3;
    if (C[i] == D[i])
      S[A[i]] += 1, S[B[i]] += 1;
  }

  int T = 0;
  for (int i = 1; i < N + 1; i++) {
    T = 1;
    for (int j = 1; j < N + 1; j++) {
      if (S[i] < S[j])
        T += 1;
    }
    cout << T << endl;
  }
  return 0;
}

Fixed submission:
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
int A[5001], B[5001], C[5001], D[5001];
int N, M;
int S[101] = {};
int main() {
  cin >> N;
  for (int i = 0; i < (N * (N - 1) / 2); i++) {
    cin >> A[i] >> B[i] >> C[i] >> D[i];
    if (C[i] > D[i])
      S[A[i]] += 3;
    if (C[i] < D[i])
      S[B[i]] += 3;
    if (C[i] == D[i])
      S[A[i]] += 1, S[B[i]] += 1;
  }

  int T = 0;
  for (int i = 1; i < N + 1; i++) {
    T = 1;
    for (int j = 1; j < N + 1; j++) {
      if (S[i] < S[j])
        T += 1;
    }
    cout << T << endl;
  }
  return 0;
}
Problem p00489 (C++)

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

using namespace std;

int main(void) {
  int k, n, a[101], b[101], c[101], d[101], t[101] = {0};
  int cnt;
  cin >> n;
  k = n * (n - 1) / 2;
  for (int i = 0; i < k; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (int i = 0; i < k; i++) {
    if (c[i] > d[i])
      t[a[i]] += 3;
    else if (c[i] < d[i])
      t[b[i]] += 3;
    else {
      t[a[i]] += 1;
      t[b[i]] += 1;
    }
  }

  for (int i = 1; i <= n; i++) {
    cnt = 1;
    for (int j = 1; j <= n; j++) {
      if (t[i] < t[j])
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}


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

using namespace std;

int main(void) {
  int k, n, a[5001], b[5001], c[5001], d[5001], t[5001] = {0};
  int cnt;
  cin >> n;
  k = n * (n - 1) / 2;
  for (int i = 0; i < k; i++)
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  for (int i = 0; i < k; i++) {
    if (c[i] > d[i])
      t[a[i]] += 3;
    else if (c[i] < d[i])
      t[b[i]] += 3;
    else {
      t[a[i]] += 1;
      t[b[i]] += 1;
    }
  }

  for (int i = 1; i <= n; i++) {
    cnt = 1;
    for (int j = 1; j <= n; j++) {
      if (t[i] < t[j])
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}