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

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

using namespace std;

int n, k;
vector<vector<int>> book_price;
vector<vector<int>>
    pricesum; //?????????????????¬????????£???????????§?????????????´???????
int dp[2001][11]; // num genre

int main() {
  book_price.resize(10);
  pricesum.resize(10);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    book_price[b - 1].push_back(a);
  }
  for (int i = 0; i < 10; i++) {
    sort(book_price[i].begin(), book_price[i].end(),
         greater<int>()); //??§?????????????????????
    pricesum[i].push_back(0);
    for (auto j = 0; j < book_price[i].size(); j++) {
      pricesum[i].push_back(book_price[i][j] + j * 2);
      if (j != 0)
        pricesum[i][j + 1] +=
            pricesum[i][j]; //??§?????????????´????????????¨???
    }
  }
  int ct = 0;
  for (int i = 0;; i++) {
    if (pricesum[i].size() == 1) {
      pricesum.erase(pricesum.begin() + i), i--; //????????¨????????????
    }
    ct++;
    if (ct >= 10)
      break;
  }
  for (int i = 0; i <= k; i++) {
    if (i < pricesum[0].size())
      dp[i][0] = pricesum[0][i];
    else
      dp[i][0] = dp[i - 1][0];
  }
  for (int i = 0; i < pricesum.size() - 1; i++) {
    for (int j = 0; j <= k; j++) {
      for (int l = 0; l < pricesum[i + 1].size(); l++) {
        if (j + l <= k)
          dp[j + l][i + 1] =
              max(dp[j + l][i + 1], dp[j][i] + pricesum[i + 1][l]);
      }
    }
  }
  cout << dp[k][pricesum.size() - 1] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

int n, k;
vector<vector<int>> book_price;
vector<vector<int>>
    pricesum; //?????????????????¬????????£???????????§?????????????´???????
int dp[2001][11]; // num genre

int main() {
  book_price.resize(10);
  pricesum.resize(10);
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    book_price[b - 1].push_back(a);
  }
  for (int i = 0; i < 10; i++) {
    sort(book_price[i].begin(), book_price[i].end(),
         greater<int>()); //??§?????????????????????
    pricesum[i].push_back(0);
    for (auto j = 0; j < book_price[i].size(); j++) {
      pricesum[i].push_back(book_price[i][j] + j * 2);
      if (j != 0)
        pricesum[i][j + 1] +=
            pricesum[i][j]; //??§?????????????´????????????¨???
    }
  }
  int ct = 0;
  for (int i = 0;; i++) {
    if (pricesum[i].size() == 1) {
      pricesum.erase(pricesum.begin() + i), i--; //????????¨????????????
    }
    ct++;
    if (ct >= 10)
      break;
  }
  dp[0][0] = 0;
  for (int i = 1; i <= k; i++) {
    if (i <= pricesum[0].size())
      dp[i][0] = pricesum[0][i - 1];
    else
      dp[i][0] = dp[i - 1][0];
  }
  for (auto i = 0; i < pricesum.size(); i++) { // genre
    dp[0][i] = 0;
    for (int l = 0; l < pricesum[i].size(); l++) { // addnum
      dp[l][i + 1] = max(dp[l][i + 1], dp[0][i] + pricesum[i][l]);
    }
    for (int j = 1; j <= k; j++) { // num
      if (j > pricesum[i].size())
        dp[j][i] = dp[j - 1][i];
      else {
        for (int l = 0; l < pricesum[i].size(); l++) { // addnum
          dp[j + l][i + 1] = max(dp[j + l][i + 1], dp[j][i] + pricesum[i][l]);
        }
      }
    }
  }
  cout << dp[k][pricesum.size()] << endl;
  return 0;
}