alexjercan/bugnet huggingface dataset:
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <fstream>
#include <iostream>
using namespace std;

char map[1010][1010];
int sum[1010][1010][3] = {0};

int conv(char c) {

  if (c == 'J') {
    return 0;
  } else if (c == 'O') {
    return 1;
  } else if (c == 'I') {
    return 2;
  } else {
    return -1;
  }
}

void calc(int a, int b, int c, int d) {

  int J = sum[c][d][0] - sum[a - 1][d][0] - sum[c][b - 1][0] +
          sum[a - 1][b - 1][0],
      O = sum[c][d][1] - sum[a - 1][d][1] - sum[c][b - 1][1] +
          sum[a - 1][b - 1][1],
      I = sum[c][d][2] - sum[a - 1][d][2] - sum[c][b - 1][2] +
          sum[a - 1][b - 1][2];
  cout << J << " " << O << " " << I << endl;
}

int main() {

  // ifstream cin("in/01.in");
  // ofstream cout("OUTPUT.txt");

  int M, N, K;

  cin >> M >> N >> K;

  for (int i = 0; i < M; i++) {
    cin >> map[i];
  }

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      for (int k = 0; k < 3; k++) {
        sum[i][j][k] = sum[i - 1][j][k] - sum[i - 1][j - 1][k] +
                       sum[i][j - 1][k] + (conv(map[i - 1][j - 1]) == k);
      }
    }
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    calc(a, b, c, d);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <fstream>
#include <iostream>
using namespace std;

char map[1010][1010];
int sum[1010][1010][3] = {0};

int conv(char c) {

  if (c == 'J') {
    return 0;
  } else if (c == 'O') {
    return 1;
  } else if (c == 'I') {
    return 2;
  } else {
    return -1;
  }
}

void calc(int a, int b, int c, int d) {
  int ans[3];
  bool w = a > 1, ww = b > 1;

  for (int i = 0; i < 3; i++) {
    ans[i] = sum[c - 1][d - 1][i];
    if (w)
      ans[i] -= sum[a - 2][d - 1][i];
    if (ww)
      ans[i] -= sum[c - 1][b - 2][i];
    if (w || ww)
      ans[i] += sum[a - 1 - w][b - 1 - ww][i];
  }
  cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
}

int main() {

  // ifstream cin("INPUT.txt");
  // ofstream cout("OUTPUT.txt");

  int M, N, K;

  cin >> M >> N >> K;

  for (int i = 0; i < M; i++) {
    cin >> map[i];
  }

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < 3; k++) {
        if (i > 0) {
          sum[i][j][k] += sum[i - 1][j][k];
          if (j > 0)
            sum[i][j][k] -= sum[i - 1][j - 1][k];
        }
        if (j > 0) {
          sum[i][j][k] += sum[i][j - 1][k];
        }
        if (k == conv(map[i][j])) {
          sum[i][j][k]++;
        }
      }
    }
  }

  for (int i = 0; i < K; K++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    calc(a, b, c, d);
  }

  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i < k; i++)
typedef long long ll;
typedef unsigned long long ull;
char field[1024][1024];
int dp[1024][1024][3];
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  int k;
  scanf("%d", &k);

  rep(i, m) {
    scanf("%s", field[i]);
    rep(j, n) {
      if (field[i][j] == 'J') {
        dp[i][j][0] += (j == 0) ? 1 : dp[i][j - 1][0] + 1;
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
      } else if (field[i][j] == 'O') {
        dp[i][j][0] = dp[i][j - 1][0];
        dp[i][j][1] += (j == 0) ? 1 : dp[i][j - 1][1] + 1;
        dp[i][j][2] = dp[i][j - 1][2];
      } else {
        dp[i][j][0] = dp[i][j - 1][0];
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] += (j == 0) ? 1 : dp[i][j - 1][2] + 1;
      }
    }
  }
  while (k--) {
    int y1, x1, y2, x2;
    int J = 0, O = 0, I = 0;
    scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
    reps(i, y1 - 1, y2) {
      J += dp[i][x2 - 1][0] - ((x1 - 2 < 0) ? 0 : dp[i][x1 - 2][0]);
      O += dp[i][x2 - 1][1] - ((x1 - 2 < 0) ? 0 : dp[i][x1 - 2][1]);
      I += dp[i][x2 - 1][2] - ((x1 - 2 < 0) ? 0 : dp[i][x1 - 2][2]);
    }
    printf("%d %d %d\\n", J, O, I);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
#define INF 1e+8
#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i < k; i++)
typedef long long ll;
typedef unsigned long long ull;
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  int k;
  scanf("%d", &k);
  char field[1024][1024];
  rep(i, m) { scanf("%s", field[i]); }
  while (k--) {
    int y1, x1, y2, x2;
    int J = 0, O = 0, I = 0;
    scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
    reps(i, y1 - 1, y2) {
      reps(j, x1 - 1, x2) {
        if (field[i][j] == 'J')
          J++;
        else if (field[i][j] == 'O')
          O++;
        else
          I++;
      }
    }
    printf("%d %d %d\\n", J, O, I);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
struct P {
  ll j, o, i;
};
P operator+(P a, P b) { return P{a.j + b.j, a.o + b.o, a.i + b.i}; }
P operator-(P a, P b) { return P{a.j - b.j, a.o - b.o, a.i - b.i}; }
char a[1000][1000];
P b[1000][1000];
int main() {
  int h, w, n;
  cin >> h >> w >> n;
  for (int i = 0; i < h; i++) {
    string u;
    cin >> u;
    for (int j = 0; j < w; j++) {
      a[i][j] = u[j];
      if (j == 0) {
        if (u[0] == 'J')
          b[i][j] = P{1, 0, 0};
        else if (u[0] == 'O')
          b[i][j] = P{0, 1, 0};
        else
          b[i][j] = P{0, 0, 1};
      } else {
        b[i][j] = b[i][j - 1];
        if (u[j] == 'J')
          b[i][j].j++;
        else if (u[j] == 'O')
          b[i][j].o++;
        else
          b[i][j].i++;
      }
    }
  }
  for (int z = 0; z < n; z++) {
    int a, e, c, d;
    cin >> a >> e >> c >> d;
    a--;
    e--;
    c--;
    d--;
    P sum = {0, 0, 0};
    for (int i = a; i <= c; i++) {
      if (e == 0)
        sum = sum + (b[i][d]);
      else
        sum = sum + (b[i][d] - b[i][e - 1]);
    }
    cout << sum.j << " " << sum.o << " " << sum.i << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int e[1000][1000];
int f[1000][100][3];
int main() {
  int h, w, k;
  cin >> h >> w;
  cin >> k;
  int sum = 0;
  int h2 = floor(sqrt(w));
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == 'J')
        e[i][j] = 0;
      else if (s[j] == 'O')
        e[i][j] = 1;
      else
        e[i][j] = 2;
    }
    sum = 0;
    for (int j = 0; j < floor(w / h2); j++) {
      f[i][j][0] = 0;
      f[i][j][1] = 0;
      f[i][j][2] = 0;
      for (int z = j * h2; z < (j + 1) * h2; z++) {
        f[i][j][e[i][z]]++;
      }
    }
  }
  for (int z = 0; z < k; z++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    c--;
    d--;
    int sum[3] = {0, 0, 0};
    for (int i = a; i <= c; i++) {
      for (int j = b; j <= d; j++) {
        if (j % h2 == 0 && j + h2 - 1 <= d) {
          sum[0] += f[i][j / h2][0];
          sum[1] += f[i][j / h2][1];
          sum[2] += f[i][j / h2][2];
          j += (h2 - 1);
        } else {
          sum[e[i][j]]++;
        }
      }
    }
    cout << sum[0] << " " << sum[1] << " " << sum[2] << endl;
  }
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error


#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

#define J 0
#define O 1
#define I 2

int H, W, N;
string c = "JOI";
char s[1001][1001];
int mem[1001][1001][3];

int main() {
  cin >> H >> W >> N;
  for (int i = 1; i <= H; i++)
    scanf("%s", s[i] + 1);

  for (int i = 0; i < 3; i++) {
    mem[1][1][i] = c[i] == s[1][1];
    for (int x = 2; x <= W; x++)
      mem[x][1][i] = mem[x - 1][1][i] + (s[1][x] == c[i] ? 1 : 0);
    for (int y = 2; y <= H; y++)
      mem[1][y][i] = mem[1][y - 1][i] + (s[y][1] == c[i] ? 1 : 0);
  }
  for (int i = 0; i < 3; i++)
    for (int x = 2; x <= W; x++)
      for (int y = 2; y <= H; y++)
        mem[x][y][i] = mem[x - 1][y][i] + mem[x][y - 1][i] -
                       mem[x - 1][y - 1][i] + (s[y][x] == c[i] ? 1 : 0);
  for (int i = 0; i < N; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    for (int j = 0; j < 3; j++)
      cout << mem[d][c][j] + mem[b - 1][a - 1][j] - mem[b - 1][c][j] -
                  mem[d][a - 1][j]
           << (j < 2 ? " " : "");
    cout << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

char c[1010][1010];
int d[1010][1010][3] = {{{0}, {0}}};

const int J = 0;
const int O = 1;
const int I = 2;

int main(void) {
  int m, n, k;
  cin >> m >> n >> k;
  for (int i = 0; i < m; i++) {
    scanf("%s", c[i]);
  }

  for (int y = 0; y < m; y++) {
    for (int x = 0; x < n; x++) {
      char t = c[y][x];
      if (!x && !y) {
      } else if (!x) {
        d[y][x][J] = d[y - 1][x][J];
        d[y][x][O] = d[y - 1][x][O];
        d[y][x][I] = d[y - 1][x][I];
      } else if (!y) {
        d[y][x][J] = d[y][x - 1][J];
        d[y][x][O] = d[y][x - 1][O];
        d[y][x][I] = d[y][x - 1][I];
      } else {
        d[y][x][J] = d[y - 1][x][J] + d[y][x - 1][J] - d[y - 1][x - 1][J];
        d[y][x][O] = d[y - 1][x][O] + d[y][x - 1][O] - d[y - 1][x - 1][O];
        d[y][x][I] = d[y - 1][x][I] + d[y][x - 1][I] - d[y - 1][x - 1][I];
      }
      if (t == 'J')
        d[y][x][J]++;
      else if (t == 'O')
        d[y][x][O]++;
      else
        d[y][x][I]++;
    }
  }

  for (int i = 0; i < k; i++) {
    int y1, x1, y2, x2;
    cin >> y1 >> x1 >> y2 >> x2;
    y1--;
    y1--;
    x1--;
    x1--;
    y2--;
    x2--;
    int ans[3] = {0};
    ans[J] = d[y2][x2][J] - d[y1][x2][J] - d[y2][x1][J] + d[y1][x1][J];
    ans[O] = d[y2][x2][O] - d[y1][x2][O] - d[y2][x1][O] + d[y1][x1][O];
    ans[I] = d[y2][x2][I] - d[y1][x2][I] - d[y2][x1][I] + d[y1][x1][I];
    printf("%d %d %d\\n", ans[J], ans[O], ans[I]);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)

char s[1001];
int l[3][1001][1001];
int main() {
  int n, m, k, a, b, c, d, cnt[3];
  const char key[] = {'J', 'O', 'I'};
  scanf("%d%d%d", &m, &n, &k);
  reps(i, 1, m) {
    scanf("%s", s);
    rep(j, 3) cnt[j] = 0;
    reps(j, 1, n) {
      rep(k, 3) {
        if (s[j - 1] == key[k])
          cnt[k]++;
        l[k][i][j] = cnt[k] + l[k][i - 1][j];
      }
    }
  }
  rep(i, k) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    a--;
    b--;
    rep(j, 3) cnt[j] = l[j][c][d] + l[j][a][b] - l[j][a][d] - l[j][c][b];
    printf("%d %d %d\\n", cnt[0], cnt[1], cnt[2]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)

char s[1001][1001];

int main() {
  int n, m, k, a, b, c, d, J, O, I;
  scanf("%d%d%d", &m, &n, &k);
  rep(i, m) scanf("%s", s[i]);
  rep(i, k) {
    scanf("%d%d%d%d", &a, &b, &c, &d);
    a--;
    b--;
    c--;
    d--;
    J = O = I = 0;
    reps(i, a, c) reps(j, b, d) {
      if (s[i][j] == 'J')
        J++;
      else if (s[i][j] == 'O')
        O++;
      else if (s[i][j] == 'I')
        I++;
    }
    printf("%d %d %d\\n", J, O, I);
  }
  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
using namespace std;

char ma[1010][1010];
int jmap[1010][1010];
int omap[1010][1010];
int imap[1010][1010];

int main(void) {
  int M, N, K;

  scanf("%d %d %d", &M, &N, &K);
  for (int i = 0; i < M; i++) {
    scanf("%s", ma[i]);
  }

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      jmap[i][j] = 0;
      omap[i][j] = 0;
      imap[i][j] = 0;
    }
  }

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      jmap[i][j] = jmap[i - 1][j] + jmap[i][j - 1] - jmap[i - 1][j - 1];
      omap[i][j] = omap[i - 1][j] + omap[i][j - 1] - omap[i - 1][j - 1];
      imap[i][j] = imap[i - 1][j] + imap[i][j - 1] - imap[i - 1][j - 1];

      if (ma[i - 1][j - 1] == 'J')
        jmap[i][j]++;
      else if (ma[i - 1][j - 1] == 'O')
        omap[i][j]++;
      else if (ma[i - 1][j - 1] == 'I')
        imap[i][j]++;
    }
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);

    int jans =
        jmap[c][d] - jmap[c][b - 1] - jmap[a - 1][d] + jmap[a - 1][b - 1];
    int oans =
        omap[c][d] - omap[c][b - 1] - omap[a - 1][d] + omap[a - 1][b - 1];
    int ians =
        imap[c][d] - imap[c][b - 1] - imap[a - 1][d] + imap[a - 1][b - 1];

    printf("%d %d %d\\n", jans, oans, ians);
  }

  return 0;
}

Error: -11

Fixed submission:

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

int main(void) {
  int M, N, K;
  char map[1010][1010];
  int jmap[1010][1010];
  int omap[1010][1010];
  int imap[1010][1010];

  scanf("%d %d %d", &M, &N, &K);
  for (int i = 0; i < M; i++) {
    scanf("%s", map[i]);
  }

  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      jmap[i][j] = 0;
      omap[i][j] = 0;
      imap[i][j] = 0;
    }
  }

  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      jmap[i][j] = jmap[i - 1][j] + jmap[i][j - 1] - jmap[i - 1][j - 1];
      omap[i][j] = omap[i - 1][j] + omap[i][j - 1] - omap[i - 1][j - 1];
      imap[i][j] = imap[i - 1][j] + imap[i][j - 1] - imap[i - 1][j - 1];

      if (map[i - 1][j - 1] == 'J')
        jmap[i][j]++;
      else if (map[i - 1][j - 1] == 'O')
        omap[i][j]++;
      else if (map[i - 1][j - 1] == 'I')
        imap[i][j]++;
    }
  }

  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);

    int jans =
        jmap[c][d] - jmap[c][b - 1] - jmap[a - 1][d] + jmap[a - 1][b - 1];
    int oans =
        omap[c][d] - omap[c][b - 1] - omap[a - 1][d] + omap[a - 1][b - 1];
    int ians =
        imap[c][d] - imap[c][b - 1] - imap[a - 1][d] + imap[a - 1][b - 1];

    printf("%d %d %d\\n", jans, oans, ians);
  }

  return 0;
}
Problem p00483 (C++) — buggy submission, verdict: 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;

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

Error: TLE

Fixed submission:

#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;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

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

int N, K;
deque<int> A[10];

int dp[11][2222];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    int C, G;
    cin >> C >> G;
    A[G - 1].push_back(C);
  }
  for (int i = 0; i < 10; ++i) {
    sort(A[i].begin(), A[i].end(), greater<int>());
    A[i].push_front(0);
    for (int j = 1; j < A[i].size(); ++j) {
      A[i][j] += A[i][j - 1];
    }
  }
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j <= K; ++j) {
      int &now = dp[i][j];
      for (int k = 0; k < A[i].size() && j + k <= K; ++k) {
        int &next = dp[i + 1][j + k];
        next = max(next, now + A[i][k] + k * (k - 1));
      }
    }
  }
  cout << dp[10][K] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int N, K;
deque<int> A[11];

int dp[11][2222];

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    int C, G;
    cin >> C >> G;
    A[G - 1].push_back(C);
  }
  for (int i = 0; i < 11; ++i) {
    sort(A[i].begin(), A[i].end(), greater<int>());
    A[i].push_front(0);
    for (int j = 1; j < A[i].size(); ++j) {
      A[i][j] += A[i][j - 1];
    }
  }
  for (int i = 0; i < 11; ++i) {
    for (int j = 0; j <= K; ++j) {
      int &now = dp[i][j];
      for (int k = 0; k < A[i].size() && j + k <= K; ++k) {
        int &next = dp[i + 1][j + k];
        next = max(next, now + A[i][k] + k * (k - 1));
      }
    }
  }
  cout << dp[10][K] << endl;
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Memory Limit Exceeded


#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define BP                                                                     \\
  while (1) {                                                                  \\
  }
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

int N, K, dp[11][2005];
vector<int> b[11];

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

  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    int tc, tg;
    cin >> tc >> tg;
    b[tg].PB(tc);
  }
  for (int i = 1; i <= 10; ++i) {
    sort(b[i].begin(), b[i].end(), greater<int>());

    for (int j = 1; j < b[i].size(); ++j) {
      b[i][j] += b[i][j - 1] + 2 * (j + 1) - 2;
    }
  }

  for (int i = 1; i <= 10; ++i) {
    for (int j = 0; j <= K; ++j) {
      int tmax = 0;
      for (int i2 = 1; i2 < i; ++i2) {
        tmax = max(tmax, dp[i2][j]);
      }

      for (int k = 1; k <= b[i].size(); ++k) {
        if (K < j + k)
          continue;

        dp[i][j + k] = max(dp[i][j + k], tmax + b[i][k - 1]);
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= 10; ++i) {
    ans = max(ans, dp[i][K]);
  }

  cout << ans << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define PB push_back
// #define BP while(1){}
typedef pair<int, int> pii;
// static const int INF = 1LL << 61;

int N, K, dp[2005][11][2005];
vector<int> g[11];

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

  cin >> N >> K;

  for (int i = 0; i < N; ++i) {
    int tc, tg;
    cin >> tc >> tg;
    g[tg].PB(tc);
  }

  for (int i = 1; i <= 10; ++i)
    sort(g[i].begin(), g[i].end(), greater<int>());

  for (int i = 1; i <= 10; ++i) {
    for (int j = 1; j < g[i].size(); ++j) {
      g[i][j] += 2 * (j + 1) - 2;
    }
  }

  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= 10; ++j) {
      for (int k = 0; k < g[j].size(); ++k) {
        dp[i + 1][j][k + 1] = max(dp[i + 1][j][k + 1], dp[i][j][k] + g[j][k]);
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][k]);
      }
    }
  }

  int ans = 0;
  for (int i = 1; i <= 10; ++i) {
    for (int j = 0; j < g[i].size(); ++j) {
      ans = max(ans, dp[K + 1][i][j]);
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: 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[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;
}

Error: 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[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;
}