alexjercan/bugnet huggingface dataset:
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

// Solution for aoj 0561:Books
#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
int n, k, x, y, i, j, save2, sum, roop[11], save3;
int books[11][2001], dp[11][2001], save, books2[11][2001];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= 10; i++)
    roop[i] = 1;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    books[y][roop[y]++] = x;
  }
  for (i = 1; i <= 10; i++) {
    sort(books[i], books[i] + roop[i], greater<int>());
  }
  dp[1][1] = books[1][0];
  for (j = 2; j <= roop[1]; j++) {
    dp[1][j] = dp[1][j - 1] + books[1][j - 1];
    if (books[1][j])
      dp[1][j] += j * 2 - 2;
  }
  for (i = 1; i <= 10; i++) {
    books2[i][1] = books[i][0];
    for (j = 2; j < roop[i]; j++) {
      books2[i][j] = books2[i][j - 1] + books[i][j - 1];
      if (books[i][j - 1])
        books2[i][j] += j * 2 - 2;
    }
  }
  for (i = 2; i <= 10; i++) {
    save = 1;
    for (j = 1; j <= k; j++) {
      x = j;
      y = 0;
      for (int l = 0; l < j; l++) {
        dp[i][j] = max(dp[i - 1][x] + books2[i][y], dp[i][j]);
        x--;
        y++;
        dp[i][j] = max(dp[i - 1][x] + books2[i][y], dp[i][j]);
      }
    }
  }
  cout << dp[10][k] << endl;
  return 0;
}

Error: 0

Fixed submission:

// Solution for aoj 0561:Books
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
int n, k, a[11], fir[11], i, j, ma, num, ans, save[2001][2], sum;
int books[2001][3], books2[2001][11], books3[2001], books4[11];
void solve(int i) {
  for (i; i <= k; i++) {
    ma = 0;
    for (int j = 1; j <= 10; j++) {
      if (ma < books2[books4[j]][j] + books4[j] * 2 - 2) {
        ma = books2[books4[j]][j] + books4[j] * 2 - 2;
        num = j;
      } else if (ma == books2[books4[j]][j] + books4[j] * 2 - 2) {
        save[i][0] = books4[j];
        save[i][1] = sum;
        books4[j]++;
        sum += ma;
        solve(i + 1);
        books4[j] = save[i][0];
        sum = save[i][1];
      }
    }
    books4[num]++;
    sum += ma;
  }
  ans = max(ans, sum);
}
int main() {
  cin >> n >> k;
  for (i = 1; i <= n; i++) {
    cin >> books[i][0] >> books[i][1];
    if (fir[books[i][1]] == 0)
      fir[books[i][1]] = i;
    books[a[books[i][1]]][2] = i;
    a[books[i][1]] = i;
  }
  for (i = 1; i <= 10; i++) {
    if (a[i])
      books[a[i]][2] = -1;
    books4[i] = 1;
  }
  for (i = 1; i <= 10; i++) {
    int roop = 1;
    j = fir[i];
    if (j == 0)
      break;
    while (j != -1) {
      books3[roop] = books[j][0];
      roop++;
      j = books[j][2];
    }
    sort(books3 + 1, books3 + roop + 1, greater<int>());
    for (int l = 1; l < roop; l++) {
      books2[l][i] = books3[l];
    }
  }
  solve(1);
  cout << ans << endl;
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<vector<int>> book(10);
  for (int i = 0; i < N; ++i) {
    int C, G;
    cin >> C >> G;
    --G;
    book[G].push_back(C);
  }
  for (auto &b : book)
    sort(begin(b), end(b), greater<int>());
  for (auto &b : book)
    for (int i = 1; i < b.size(); ++i)
      b[i] += b[i - 1] + i + i;
  for (auto &b : book)
    b.insert(begin(b), 0);

  vector<vector<int>> dp(2, vector<int>(K + 1));
  auto cur = dp[0], nex = dp[1];
  for (const auto &b : book) {
    for (int i = 0; i < b.size(); ++i)
      for (int j = i; j <= K; ++j)
        nex[j] = max(nex[j], cur[j - i] + b[i]);
    swap(cur, nex);
  }
  cout << cur[K] << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> book[10];
  int answer = 0;
  for (int i = 0; i < N; ++i) {
    int C, G;
    cin >> C >> G;
    --G;
    book[G].push_back(C);
    answer += C;
  }
  for (int i = 0; i < N; ++i)
    answer += (book[i].size() - 1) * book[i].size();
  for (auto &i : book)
    sort(begin(i), end(i), greater<int>());

  vector<int> sell(10);
  for (int i = 0; i < N; ++i)
    sell[i] = book[i].size();
  for (int total = N; K < total; --total) {
    int min_group = -1;
    int min_price = 1e9;
    for (int i = 0; i < N; ++i)
      if (sell[i]) {
        int price = book[i][sell[i] - 1];
        if (sell[i] != 1)
          price += (sell[i] - 1) + (sell[i] - 1);
        if (min_price <= price)
          continue;
        min_price = price;
        min_group = i;
      }
    answer -= min_price;
    --sell[min_group];
  }

  cout << answer << endl;
}
Problem p00484 (C++) — buggy submission, verdict: 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 < 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;
}


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

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;

int N, K;
int memo[16][2048];
vector<int> costs[16];

int rec(int i, int used) {
  if (memo[i][used] != -1)
    return memo[i][used];

  if (i == 10)
    return 0;

  int best = 0;
  for (int t = 0; t < costs[i].size() && used + t <= K; t++) {
    best = max(best, rec(i + 1, used + t) + costs[i][t] + (t - 1) * t);
  }

  return memo[i][used] = best;
}

int main() {
  // memoを全て-1で初期化 この関数は-1以外では0でしか初期化出来ないので注意
  memset(memo, -1, sizeof memo);

  cin >> N >> K;

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

  for (int i = 0; i < 10; i++) {
    sort(books[i].begin(), books[i].end(), greater<int>());

    costs[i].push_back(0);
    for (int j = 0; j < books[i].size(); j++) {
      costs[i].push_back(costs[i][j] + books[i][j]);
    }
  }

  cout << rec(0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T> T RoundOff(T a) { return int(a + .5 - (a < 0)); }
template <class T, class C> void chmax(T &a, C b) {
  if (a < b)
    a = b;
}
template <class T, class C> void chmin(T &a, C b) {
  if (b < a)
    a = b;
}
template <class T, class C> pair<T, C> mp(T a, C b) { return make_pair(a, b); }

int table[10][2000];
int len[10];

int rec(vector<vint> &cost, int p, int k) {
  if (p == 10)
    return 0;

  // if(0 <= table[p][k]) return table[p][k];

  int res = 0;
  rep(t, min(k, len[p]) + 1) {
    chmax(res, (t == 0 ? 0 : cost[p][t - 1] + (t - 1) * t) +
                   rec(cost, p + 1, k - t));
  }

  return table[p][k] = res;
}

int main() {
  memset(table, -1, sizeof(table));
  int n, k;
  cin >> n >> k;

  vector<vint> cost(10);
  rep(i, n) {
    int c, g;
    cin >> c >> g;

    cost[g - 1].push_back(c);
  }

  rep(i, 10) {
    len[i] = cost[i].size();
    sort(ALL(cost[i]), greater<int>());
    partial_sum(ALL(cost[i]), cost[i].begin());
  }

  cout << rec(cost, 0, k) << endl;
}
Problem p00484 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <utility>

using namespace std;

const int MAX_N = 2000, MAX_G = 10;
int N, K;
pair<int, int> book[MAX_N]; // first=G,second=C
int genre[MAX_G + 1];
int genre2[MAX_G + 1]; //????????????????????£???????????¬??????????????????
int table[MAX_G + 1][MAX_N + 1];
int dp[MAX_G + 1][MAX_N + 1];

void solve();

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    int C, G;
    scanf("%d %d", &C, &G);
    book[i] = make_pair(G, C);
  }
  solve();
  return 0;
}

void solve() {
  sort(book, book + N);
  // table??????????????????
  int i, j, k;
  for (i = 0; i < N; i++) {
    genre2[book[i].first]++;
  }
  genre[1] = genre2[1];
  for (i = 2; i <= MAX_G; i++) {
    genre[i] = genre[i - 1] + genre2[i];
  }
  for (i = 1; i <= MAX_G; i++) {
    for (j = genre[i] - 1; j >= genre[i - 1]; --j) {
      table[i][genre[i] - j] = table[i][genre[i] - j - 1] + book[j].second;
    }
  }
  for (i = 0; i < MAX_G; i++) {
    //?????¨???????£?????????¨??????????????????
    for (j = 2; j <= genre2[i + 1]; j++) {
      table[i + 1][j] += j * (j - 1);
    }
  }
  //???????¨?????????§?§£????±???????
  for (i = 0; i < MAX_G; i++) {
    for (j = 0; j <= K; j++) {
      for (k = 0; k <= genre2[i + 1] && j + k <= K; k++) {
        dp[(i + 1)][j + k] =
            max(dp[(i + 1)][j + k], dp[i][j] + table[(i + 1)][k]);
      }
    }
  }
  printf("%d\\n", dp[MAX_G][K]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

const int MAX_N = 2000, MAX_G = 10;

int N, K;
int C[MAX_N], G[MAX_N];

bool flg[MAX_N]; // flg[i]=true:i????£????
int cnt[MAX_G];
int books; //?£?????????°
int dfs(int);

int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", C + i, G + i);
  }
  printf("%d\\n", dfs(0));
  return 0;
}

int dfs(int n) {
  // n????????¶????????????
  int res = 0;
  if (books == K) {
    //???????????????????????£???
    // cnt????¨????????????????
    fill(cnt, cnt + MAX_G, 0);
    for (int i = 0; i < N; i++) {
      if (flg[i]) {
        cnt[G[i] - 1]++;
      }
    }
    for (int i = 0; i < N; i++) {
      if (flg[i]) {
        res += C[i] + cnt[G[i] - 1] - 1;
      }
    }
    return res;
  }
  if (n == N) {
    return 0;
  }
  res = dfs(n + 1); //?????°????????£?????´???
  //???????????´???
  flg[n] = true;
  books++;
  res = max(res, dfs(n + 1));
  flg[n] = false;
  books--;
  return res;
}
Problem p00484 (C++) — buggy submission, verdict: 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;
      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;
}

Error: 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;
      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, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

constexpr int MAX_N = 2001;

using ll = long long int;

int n, k, maxLabel;

std::vector<int> books[11];

ll memo[11][MAX_N];

ll solve(int label, int m) {
  if (memo[label][m] > 0)
    return memo[label][m];
  if (m == 0)
    return 0;
  if (label >= maxLabel)
    return 0;

  ll res = solve(label + 1, m);

  for (int i = 0; i < books[label].size() && i < m; ++i)
    res = std::max(res,
                   solve(label + 1, m - i - 1) + books[label][i] + i * (i + 1));

  return memo[label][m] = res;
}

int main() {
  std::cin >> n >> k;
  int c, g;
  for (int i = 0; i < n; ++i) {
    std::cin >> c >> g;
    books[g - 1].push_back(c);
    maxLabel = std::max(maxLabel, g);
  }
  for (int i = 0; i < 10; ++i)
    std::sort(books[i].begin(), books[i].end(), std::greater<int>());

  for (int i = 0; i < maxLabel; ++i) {
    for (int j = 1; j < books[i].size(); ++j)
      books[i][j] += books[i][j - 1];
  }

  std::cout << solve(0, k) << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

constexpr int MAX_N = 2000;

using ll = long long int;

int n, k, maxLabel;

std::vector<int> books[10];

ll memo[10][MAX_N];

ll solve(int label, int m) {
  if (memo[label][m] > 0)
    return memo[label][m];
  if (m == 0)
    return 0;
  if (label >= maxLabel)
    return 0;

  ll res = solve(label + 1, m);

  for (int i = 0; i < books[label].size() && i < m; ++i)
    res = std::max(res,
                   solve(label + 1, m - i - 1) + books[label][i] + i * (i + 1));

  return memo[label][m] = res;
}

int main() {
  std::cin >> n >> k;
  int c, g;
  for (int i = 0; i < n; ++i) {
    std::cin >> c >> g;
    books[g - 1].push_back(c);
    maxLabel = std::max(maxLabel, g);
  }
  for (int i = 0; i < 10; ++i)
    std::sort(books[i].begin(), books[i].end(), std::greater<int>());

  for (int i = 0; i < maxLabel; ++i) {
    for (int j = 1; j < books[i].size(); ++j)
      books[i][j] += books[i][j - 1];
  }

  std::cout << solve(0, k) << std::endl;
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
typedef pair<p, p> p2;
#define pu push
#define pb push_back
#define mp make_pair
#define INF 100000000
int value[2001];
int jan[2001];
vector<int> vec[11];
vector<int> rui[11];
int dp[2005] = {};
int main() {
  int n;
  int k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &value[i]);
    scanf("%d", &jan[i]);
  }
  for (int i = 0; i < n; i++) {
    vec[jan[i] - 1].pb(value[i]);
  }
  for (int i = 0; i < 10; i++) {
    sort(vec[i].begin(), vec[i].end(), greater<int>());
  }
  for (int i = 0; i < 10; i++) {
    rui[i] = vector<int>(vec[i].size());
    if (vec[i].size() > 0) {
      int rp = vec[i].size();
      rui[i][0] = vec[i][0];
      for (int w = 1; w < rp; w++) {
        rui[i][w] = rui[i][w - 1] + vec[i][w] + 2 * w;
      }
      for (int w = k; w >= 0; w--) {
        for (int l = 0; l < min(w, rp); l++) {
          dp[w] = max(dp[w], dp[w - l - 1] + rui[i][l]);
        }
      }
    }
  }
  printf("%d\\n", dp[k]);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> p;
typedef pair<int, p> P;
typedef pair<p, p> p2;
#define pu push
#define pb push_back
#define mp make_pair
#define INF 100000000
int value[2001];
int jan[2001];
vector<int> vec[11];
vector<int> rui[11];
int dp[2005] = {};
int main() {
  int n;
  int k;
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", &value[i]);
    scanf("%d", &jan[i]);
  }
  for (int i = 0; i < n; i++) {
    vec[jan[i]].pb(value[i]);
  }
  for (int i = 1; i <= 10; i++) {
    sort(vec[i].begin(), vec[i].end(), greater<int>());
  }
  for (int i = 1; i <= 10; i++) {
    if (vec[i].size() > 0) {
      rui[i][0] = vec[i][0];
      for (int w = 1; w < vec[i].size(); w++) {
        rui[i][w] = rui[i][w - 1] + vec[i][w] + 2 * w;
      }
      for (int w = k; w >= 0; w--) {
        for (int l = 0; l < min(w, (int)vec[i].size()); l++) {
          dp[w] = max(dp[w], dp[w - l - 1] + rui[i][l]);
        }
      }
    }
  }
  printf("%d\\n", dp[k]);
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define LL long long
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
static const LL INF = 1LL << 61LL;
static const int mod = 10007;
typedef pair<int, int> PII;
LL N, K;
LL V[11][2020];
LL Q[11][2020], T[11][2020];
LL mem[2020][11];
LL temp;

LL cal(int n, int kind) {
  LL res = 0;
  if (mem[n][kind] != INF) {
    return mem[n][kind];
  }
  if (n == K || kind > temp)
    return 0;
  REP(i, K - n) {
    if (n + i <= K) {
      if (Q[kind][i]) {
        res = max(cal(n + i + 1, kind + 1) + Q[kind][i],
                  max(cal(n, kind + 1), res));
      }
    }
  }
  return mem[n][kind] = res;
}

signed main() {
  REP(i, 2001) REP(j, 11) mem[i][j] = INF;
  cin >> N >> K;
  REP(i, N) {
    LL C, G;
    cin >> C >> G;
    V[G][i] = C;
    temp = max(temp, G);
  }
  FOR(i, 1, temp) {
    sort(V[i], V[i] + N, greater<LL>());
    REP(j, K) {
      if (!V[i][j])
        break;
      T[i][j] = j * (j + 1);
      Q[i][j] = Q[i][j - 1] + V[i][j] + T[i][j] - T[i][j - 1];
    }
  }
  cout << cal(0, 1) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
using namespace std;
static const LL INF = 1 << 50;
static const int mod = 10007;
typedef pair<int, int> PII;
int N, K;
int V[11][2020];
int Q[11][2020], T[11][2020];
int mem[2020][11];

int cal(int n, int kind) {
  int res = 0;
  if (mem[n][kind])
    return mem[n][kind];
  if (n == K || kind >= 10)
    return 0;
  REP(i, K - n) {
    if (n + i <= K) {
      if (i == 0) {
        res = max(cal(n + i + 1, kind + 1) + Q[kind][i],
                  max(cal(n, kind + 1), res));
      } else
        res = max(cal(n + i + 1, kind + 1) + Q[kind][i], res);
    }
  }
  return mem[n][kind] = res;
}

signed main() {
  cin >> N >> K;
  REP(i, N) {
    int C, G;
    cin >> C >> G;
    V[G][i] = C;
  }
  FOR(i, 1, 10) {
    sort(V[i], V[i] + N, greater<LL>());
    REP(j, K) {
      if (!V[i][j])
        break;
      T[i][j] = j * (j + 1);
      Q[i][j] = Q[i][j - 1] + V[i][j] + T[i][j] - T[i][j - 1];
    }
  }
  cout << cal(0, 1) << endl;
}