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());
  rep(i, 0, 10) {
    rep(j, 0, s[i].size()) {
      if (j == 0)
        s[i][j] = v[i][j];
      else
        s[i][j] = s[i][j - 1] + v[i][j] + 2 * j;
    }
  }

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

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

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

  cout << dp[9][K - 1] << 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());
  rep(i, 0, 10) {
    rep(j, 0, s[i].size()) {
      if (j == 0)
        s[i][j] = v[i][j];
      else
        s[i][j] = s[i][j - 1] + v[i][j] + 2 * j;
    }
  }

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

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

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

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

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