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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define mp1(a, b, c) P1(a, P(b, c))
#define pque(a) priority_queue<a>
#define rpque(a) priority_queue<a, vector<a>, greater<a>>

const int INF = 1000000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0},  {1, 1},   {0, 1},  {-1, 1},
                         {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
const int kaijou[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};

int main() {
  int n, k;
  int c[2002], g[2002];

  scanf("%d%d", &n, &k);
  rep(i, n) { scanf("%d%d", &c[i], &g[i]); }

  int dp[12][2002] = {};

  rep1(i, 10) {

    vector<int> vec;
    rep(j, n) {
      if (g[j] == i) {
        vec.pb(c[j]);
      }
    }
    sor(vec);
    rev(vec);
    rep(j, vec.size()) {
      if (j > 0) {
        vec[j] += vec[j - 1];
      }
    }
    rep(j, vec.size()) { vec[j] += j * (j + 1); }

    /*printf("%d:\\n",i);
    rep(j,vec.size()){
            printf("%d ",vec[j]);
    }
    puts("");*/

    rep(j, n + 1) {
      dp[i][j] = dp[i - 1][j];
      rep(l, min((int)vec.size(), j)) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - l - 1] + vec[l]);
      }
    }
  }

  /*rep1(i,10){
          rep(j,n+1){
                  printf("%d ",dp[i][j]);
          }
          puts("");
  }*/

  printf("%d\\n", dp[10][k]);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

bool f(int x, int y) {
  if (x >= y)
    return true;
  return false;
}

int main() {
  int n, k, dp[15][3002] = {0}, c, g;
  vector<int> a[15];
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &g, &c);
    a[c].push_back(g);
  }
  for (int i = 1; i <= 10; i++) {
    sort(a[i].begin(), a[i].end(), f);
    int s = a[i].size();
    // if(s!=0)cout<<a[i][0];
    for (int j = 1; j < s; j++) {
      a[i][j] += a[i][j - 1] + j + j;
      // cout<<a[i][j];
    }
    // cout<<"aaa";
    for (int j = 0; j <= k; j++) {
      for (int l = 0; l <= min(j, s); l++) {
        int h = 0;
        if (l != 0)
          h = a[i][l - 1];
        if (dp[i][j] < dp[i - 1][j - l] + h)
          dp[i][j] = dp[i - 1][j - l] + h;
      }
    }
    // cout<<"bbb";
  }
  printf("%d\\n", dp[10][k]);
  return 0;
}