Problem p00484 (C++)

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, K;
  cin >> n >> K;

  vector<int> v[10];
  rep(i, 0, n) {
    int c, g;
    cin >> c >> g;
    --g;
    v[g].push_back(c);
  }

  rep(i, 0, 10) sort(v[i].rbegin(), v[i].rend());

  vector<int> s[10];
  rep(i, 0, 10) {
    s[i].resize(v[i].size() + 1);
    rep(j, 1, s[i].size()) {
      s[i][j] = s[i][j - 1] + v[i][j - 1] + 2 * (j - 1);
    }
  }

  rep(i, 0, 10) dump(s[i]);

  static vector<vector<int>> dp(10, vector<int>(K + 1));
  // static int dp[10][3000];
  rep(i, 0, s[0].size()) dp[0][i] = s[0][i];
  rep(i, 1, 10) {
    rep(j, 0, s[i].size()) {
      for (int k = 0; k + j <= K; ++k) {
        dp[i][j + k] = max(dp[i][j + k], dp[i - 1][k] + s[i][j]);
      }
    }
  }

  rep(i, 0, 10) dump(dp[i]);

  cout << dp[9][K] << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int n, K;
  cin >> n >> K;

  vector<int> v[10];
  rep(i, 0, n) {
    int c, g;
    cin >> c >> g;
    --g;
    v[g].push_back(c);
  }

  rep(i, 0, 10) sort(v[i].rbegin(), v[i].rend());

  vector<int> s[10];
  rep(i, 0, 10) {
    s[i].resize(v[i].size() + 1);
    rep(j, 1, s[i].size()) {
      s[i][j] = s[i][j - 1] + v[i][j - 1] + 2 * (j - 1);
    }
  }

  rep(i, 0, 10) dump(s[i]);

  // static vector<vector<int>> dp(10,vector<int>(K+1));
  static int dp[10][3000];
  rep(i, 0, s[0].size()) dp[0][i] = s[0][i];
  rep(i, 1, 10) {
    rep(j, 0, s[i].size()) {
      for (int k = 0; k + j <= K; ++k) {
        dp[i][j + k] = max(dp[i][j + k], dp[i - 1][k] + s[i][j]);
      }
    }
  }

  rep(i, 0, 10) dump(dp[i]);

  cout << dp[9][K] << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}