Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

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

vi books[11];

int dp[11][2005]; // dp[i][j] :=
                  // i?¨?????????§???j????????£?????????????????????????????§???

int main() {
  cin >> N >> K;

  rep(i, N) {
    cin >> C[i] >> G[i];
    books[G[i]].PB(C[i]);
  }

  rep1(i, 10) {
    sort(all(books[i]), greater<int>());

    books[i].insert(begin(books[i]), 0);

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

  for (int i = 1; i <= 10; ++i) {
    for (int j = 0; j <= K; ++j) {
      for (int k = 0; j + k <= K; ++k) {
        if (k >= books[i].size())
          break;
        chmax(dp[i][j + k], dp[i - 1][j] + books[i][k]);
      }
    }
  }

  int ma = 0;

  for (int i = 0; i <= 10; ++i)
    for (int j = 0; j <= K; ++j)
      chmax(ma, dp[i][j]);

  cout << ma << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

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

vi books[10];

int dp[11][2005]; // dp[i][j] :=
                  // i?¨?????????§???j????????£?????????????????????????????§???

int main() {
  cin >> N >> K;

  rep(i, N) {
    cin >> C[i] >> G[i];
    books[G[i]].PB(C[i]);
  }

  rep(i, 10) {
    sort(all(books[i]), greater<int>());

    books[i].insert(begin(books[i]), 0);

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

  for (int i = 1; i <= 10; ++i) {
    for (int j = 0; j <= K; ++j) {
      for (int k = 0; j + k <= K; ++k) {
        if (k >= books[i].size())
          break;
        chmax(dp[i][j + k], dp[i - 1][j] + books[i][k]);
      }
    }
  }

  int ma = 0;

  for (int i = 0; i <= 10; ++i)
    for (int j = 0; j <= K; ++j)
      chmax(ma, dp[i][j]);

  cout << ma << endl;
}