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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;
int main() {
  int N, K;
  while (cin >> N >> K && N) {
    int c[2000], g[2000];
    vector<int> cash[10];
    REP(i, N) {
      cin >> c[i] >> g[i];
      g[i]--;
      cash[g[i]].push_back(c[i]);
    }
    vector<int> total[10];
    REP(i, 10) {
      sort(cash[i].begin(), cash[i].end());
      reverse(cash[i].begin(), cash[i].end());
      total[i] = vector<int>(cash[i].size() + 1);
      REP(j, cash[i].size()) total[i][j + 1] = total[i][j] + cash[i][j];
    }
    int dp[2][2000] = {};
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j <= K; j++) {
        for (int k = 0; k <= min(j, (int)cash[i].size()); k++) {
          dp[(i + 1) % 2][j] = max(
              dp[(i + 1) % 2][j], dp[i % 2][j - k] + total[i][k] + k * (k - 1));
        }
      }
    }
    cout << dp[10 % 2][K] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;
int main() {
  int N, K;
  while (cin >> N >> K && N) {
    int c[2000], g[2000];
    vector<int> cash[10];
    REP(i, N) {
      cin >> c[i] >> g[i];
      cash[g[i]].push_back(c[i]);
    }
    vector<int> total[10];
    REP(i, 10) {
      sort(cash[i].begin(), cash[i].end());
      reverse(cash[i].begin(), cash[i].end());
      total[i] = vector<int>(cash[i].size() + 1);
      REP(j, cash[i].size()) total[i][j + 1] = total[i][j] + cash[i][j];
    }
    int dp[2][2000] = {};
    for (int i = 0; i < 10; i++) {
      for (int j = 0; j <= K; j++) {
        for (int k = 0; k <= min(j, (int)cash[i].size()); k++) {
          dp[(i + 1) % 2][j] = max(
              dp[(i + 1) % 2][j], dp[i % 2][j - k] + total[i][k] + k * (k - 1));
        }
      }
    }
    cout << dp[10 % 2][K] << endl;
  }
  return 0;
}