alexjercan/bugnet huggingface dataset:
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define reps(i, j, n) for (int i = j; i < n; ++i)
#define rep(i, n) reps(i, 0, n)
vector<int> book[10];
int dp[11][2001], n, k;
/*Segmentation fault
int solve(int ct,int ja){
  if(ct >= k || ja > 10) return 0;
  if(dp[ct][ja]!=-1) return dp[ct][ja];
  int res = solve(ct,ja+1),sum = 0;
  rep(i,min(k-ct,(int)book[ja].size())){
    sum += book[ja][i];
    res = max(res,solve(ct+i+1,ja+1) + sum + i*(i+1));
  }
  return dp[ct][ja] = res;
}
*/
int main() {
  cin >> n >> k;
  rep(i, n) {
    int c, g;
    cin >> c >> g;
    book[g - 1].push_back(c);
  }
  rep(i, 10) sort(book[i].rbegin(), book[i].rend());
  rep(i, 10) rep(j, k + 1) {
    int res = 0;
    dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
    rep(k, book[i].size()) {
      res += book[i][k];
      dp[i + 1][j + k + 1] =
          max(dp[i + 1][j + k + 1], dp[i][j] + res + k * (k + 1));
    }
  }
  cout << dp[10][k] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define reps(i, j, n) for (int i = j; i < n; ++i)
#define rep(i, n) reps(i, 0, n)
vector<int> book[20];
int dp[16][2005], n, k;
int solve(int ct, int ja) {
  if (ct == k || ja > 9)
    return 0;
  if (dp[ct][ja])
    return dp[ct][ja];
  int res = solve(ct, ja + 1), sum = 0;
  rep(i, min(k - ct, (int)book[ja].size())) {
    sum += book[ja][i];
    res = max(res, solve(ct + i + 1, ja + 1) + sum + i * (i + 1));
  }
  return dp[ct][ja] = res;
}

int main() {
  cin >> n >> k;
  rep(i, n) {
    int c, g;
    cin >> c >> g;
    book[g - 1].push_back(c);
  }
  rep(i, 10) sort(book[i].rbegin(), book[i].rend());
  cout << solve(0, 0) << endl;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<int> cost[10];
int dp[15][2001];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, n) {
    int c, g;
    scanf("%d%d", &c, &g);
    cost[--g].push_back(c);
  }
  rep(i, 10) sort(cost[i].begin(), cost[i].end(), greater<int>());
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i][j] = dp[i - 1][j];
      int sum = 0;
      for (int t = 0; t < min(j, (int)cost[i - 1].size()); t++) {
        sum += cost[i - 1][t];
        dp[i][j] = max(dp[i][j], dp[i - 1][j - t - 1] + sum + t * (t + 1));
      }
    }
  }
  printf("%d\\n", dp[10][k]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

vector<int> cost[10];
int dp[10][2001];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  rep(i, n) {
    int c, g;
    scanf("%d%d", &c, &g);
    cost[--g].push_back(c);
  }
  rep(i, 10) sort(cost[i].begin(), cost[i].end(), greater<int>());
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j <= k; j++) {
      dp[i][j] = dp[i - 1][j];
      int sum = 0;
      for (int t = 0; t < min(j, (int)cost[i - 1].size()); t++) {
        sum += cost[i - 1][t];
        dp[i][j] = max(dp[i][j], dp[i - 1][j - t - 1] + sum + t * (t + 1));
      }
    }
  }
  printf("%d\\n", dp[10][k]);
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long
int dp[12][2001];
int book[11][2001];
int kind[11] = {0};
int acc[11][2001];
int main() {
  int N, K;
  int c, g;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> c >> g;
    book[g - 1][kind[g - 1]++] = c;
  }
  for (int i = 0; i < 11; i++) {
    sort(book[i], book[i] + kind[i]);
    reverse(book[i], book[i] + kind[i]);
  }
  for (int i = 0; i < 11; i++) {
    acc[i][0] = 0;
    for (int j = 1; j <= kind[i]; j++) {
      acc[i][j] = acc[i][j - 1] + book[i][j - 1] + 2 * (j - 1);
    }
  }
  for (int i = 0; i <= K; i++) {
    if (0 < i && i <= kind[0])
      dp[0][i] = acc[0][i];
    else
      dp[0][i] = 0;
  }

  for (int i = 1; i <= 11; i++) {
    for (int j = 0; j <= K; j++) {
      dp[i][j] = 0;
      for (int k = 0; k <= min(j, kind[i]); k++) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + acc[i][k]);
      }
    }
  }
  cout << dp[11][K] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
using namespace std;
#define ll long long
int dp[11][2001];
int book[11][2001];
int kind[11] = {0};
int acc[11][2001];
int main() {
  int N, K;
  int c, g;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> c >> g;
    book[g - 1][kind[g - 1]++] = c;
  }
  for (int i = 0; i < 11; i++) {
    sort(book[i], book[i] + kind[i]);
    reverse(book[i], book[i] + kind[i]);
  }
  for (int i = 0; i < 11; i++) {
    acc[i][0] = 0;
    for (int j = 1; j <= kind[i]; j++) {
      acc[i][j] = acc[i][j - 1] + book[i][j - 1] + 2 * (j - 1);
    }
  }
  for (int i = 0; i <= K; i++) {
    if (0 < i && i <= kind[0])
      dp[0][i] = acc[0][i];
    else
      dp[0][i] = 0;
  }

  for (int i = 1; i <= 11; i++) {
    for (int j = 0; j <= K; j++) {
      dp[i][j] = 0;
      for (int k = 0; k <= min(j, kind[i]); k++) {
        dp[i][j] = max(dp[i][j], dp[i - 1][j - k] + acc[i][k]);
      }
    }
  }
  cout << dp[11][K] << endl;
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;

vector<int> bok[11];
int n, k;
int memo[11][2001];
int rui[11][2001];

int check(int j, int w) { return rui[j][w] + w * (w - 1); }
int dp(int j, int w) {
  if (j == 11)
    return 0;
  if (w >= k)
    return 0;
  if (memo[j][w] != -1)
    return memo[j][w];
  int ans = 0;
  for (int i = 0; i <= min((int)bok[j].size(), k - w); i++) {
    ans = max(ans, dp(j + 1, w + i) + check(j, i));
  }
  return memo[j][w] = ans;
}

int main() {
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    bok[y].push_back(x);
  }
  memset(memo, -1, sizeof(memo));
  for (int i = 1; i < 11; i++) {
    sort(bok[i].begin(), bok[i].end());
    reverse(bok[i].begin(), bok[i].end());
    rui[i][0] = 0;
    for (int s = 1; s <= bok[i].size(); s++) {
      rui[i][s] = rui[i][s - 1] + bok[i][s - 1];
    }
  }
  cout << dp(1, 0) << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
struct books {
  int n;
  int j;
};
int n;
int k;
vector<books> bok;
int memo[2100][2100][2];
int jj[2100][2100][2][11] = {};
int check(int now, int w, int use) {
  if (now == n)
    return 0;
  if (w > k)
    return 0;
  // if (memo[now][w][use] != -1)return memo[now][w][use];
  int ans = 0;
  if (use) {
    ans += bok[now].n;
    jj[now][w][use][bok[now].j]++;
    ans += 2 * (jj[now][w][use][bok[now].j] - 1);
  }
  for (int i = 1; i < 11; i++) {
    jj[now + 1][w + 1][1][i] = jj[now][w][use][i];
    jj[now + 1][w][0][i] = jj[now][w][use][i];
  }
  ans += max(check(now + 1, w + 1, 1), check(now + 1, w, 0));

  return memo[now][w][use] = ans;
}

int main() {
  memset(memo, -1, sizeof(memo));
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    books a;
    cin >> a.n >> a.j;
    bok.push_back(a);
  }
  cout << max(check(0, 0, 0), check(0, 1, 1)) << endl;
}
Problem p00484 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define INF (1 << 29)

int N, K;
int dp[16][2049];
vector<int> b[16];

int dfs(int g, int k) {
  if (k == 0)
    return 0;
  if (k < 0)
    return -INF;
  if (g >= 10)
    return -INF;

  int &res = dp[g][k];
  if (res >= 0)
    return res;
  res = 0;
  int sum = 0;
  res = max(res, dfs(g + 1, k));
  rep(i, b[g].size()) {
    sum += b[g][i];
    res = max(res, dfs(g + 1, k - i - 1) + sum + (i * (i + 1)));
  }
  return res;
}

int main() {
  scanf("%d%d", &N, &K);
  memset(dp, -1, sizeof(dp));
  rep(i, N) {
    int c, g;
    scanf("%d%d", &c, &g);
    b[g - 1].push_back(c);
  }
  rep(i, 10) sort(b[i].begin(), b[i].end(), greater<int>());
  printf("%d\\n", dfs(0, K));
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define INF (1 << 29)

int N, K;
vector<int> b[16];

int dfs(int g, int k) {
  if (k == 0)
    return 0;
  if (k < 0)
    return -INF;
  if (g >= 10)
    return -INF;

  int res = 0;
  int sum = 0;
  res = max(res, dfs(g + 1, k));
  rep(i, b[g].size()) {
    sum += b[g][i];
    res = max(res, dfs(g + 1, k - i - 1) + sum + (i * (i + 1)));
  }
  return res;
}

int main() {
  scanf("%d%d", &N, &K);
  rep(i, N) {
    int c, g;
    scanf("%d%d", &c, &g);
    b[g - 1].push_back(c);
  }
  rep(i, 10) sort(b[i].begin(), b[i].end(), greater<int>());
  printf("%d\\n", dfs(0, K));
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> a(11);

int memo[2001][2001] = {};

int dfs(int k, int g) {
  if (memo[k][g] != -1)
    return memo[k][g];
  if (g > 10)
    return 0;
  int M = 0;
  for (int i = 0; i <= a[g].size(); i++) {
    if (k - i == 0) {
      M = max(M, a[g][i - 1]);
    } else if (k - i > 0) {
      // cout << i << " " << k << " " << g << " " << M << " " << a[g].size() <<
      // endl;
      int x = 0;
      if (i >= 1)
        x = a[g][i - 1];
      M = max(M, (memo[k - i][g + 1] = dfs(k - i, g + 1)) + x);
    }
  }
  return M;
}

int main() {
  memset(memo, -1, sizeof(memo));
  int N, K;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int C, G;
    cin >> C >> G;
    a[G].push_back(C);
  }
  for (int i = 1; i <= 10; i++) {
    sort(a[i].rbegin(), a[i].rend());
    for (int j = 1; j < a[i].size(); j++)
      a[i][j] += a[i][j - 1];
    for (int j = 1; j < a[i].size(); j++)
      a[i][j] += j * (j + 1);
    // for (int j = 0; j < a[i].size(); j++)
    //	cout << i << " " << j + 1 << " " << a[i][j] << endl;
  }
  cout << dfs(K, 1) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

vector<vector<int>> a(11);

int memo[2001][2001] = {};

int dfs(int k, int g) {
  if (g > 10)
    return 0;
  int M = 0;
  for (int i = 0; i <= a[g].size(); i++) {
    if (k - i == 0) {
      M = max(M, a[g][i - 1]);
    } else if (k - i > 0) {
      // cout << i << " " << k << " " << g << " " << M << " " << a[g].size() <<
      // endl;
      int x = 0;
      if (i >= 1)
        x = a[g][i - 1];
      M = max(M, (memo[k - i][g + 1] = dfs(k - i, g + 1)) + x);
    }
  }
  return M;
}

int main() {
  int N, K;
  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    int C, G;
    cin >> C >> G;
    a[G].push_back(C);
  }
  for (int i = 1; i <= 10; i++) {
    sort(a[i].rbegin(), a[i].rend());
    for (int j = 1; j < a[i].size(); j++)
      a[i][j] += a[i][j - 1];
    for (int j = 1; j < a[i].size(); j++)
      a[i][j] += j * (j + 1);
    // for (int j = 0; j < a[i].size(); j++)
    //	cout << i << " " << j + 1 << " " << a[i][j] << endl;
  }
  cout << dfs(K, 1) << endl;

  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 2005;

vector<int> a[11];
vector<int> b[11];
int dp[11][MAX_N];
int n, K;

int main() {
  int x, y;
  scanf("%d%d", &n, &K);
  rep(i, n) {
    scanf("%d%d", &x, &y);
    a[y - 1].push_back(x);
  }
  rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
  rep(i, 10) {
    int suma = 0;
    rep(j, a[i].size()) {
      suma += a[i][j] + 2 * j;
      b[i].push_back(suma);
    }
  }
  rep(j, K + 1) { dp[0][j] = 0; }
  rep(i, 10) {
    rep(j, K + 1) {
      dp[i + 1][j] = dp[i][j];
      for (int k = 1; k <= min((int)b[i].size(), j); k++) {
        dp[i + 1][j] =
            max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
      }
    }
  }
  printf("%d\\n", dp[10][K]);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 2005;

vector<int> a[10];
vector<int> b[10];
int dp[10][MAX_N];
int n, K;

int main() {
  int x, y;
  scanf("%d%d", &n, &K);
  rep(i, n) {
    scanf("%d%d", &x, &y);
    a[y - 1].push_back(x);
  }
  rep(i, 10) { sort(a[i].begin(), a[i].end(), greater<int>()); }
  rep(i, 10) {
    int suma = 0;
    rep(j, a[i].size()) {
      suma += a[i][j] + 2 * j;
      b[i].push_back(suma);
    }
  }
  rep(j, K + 1) { dp[0][j] = 0; }
  rep(i, 10) {
    rep(j, K + 1) {
      dp[i + 1][j] = dp[i][j];
      for (int k = 1; k <= min((int)b[i].size(), j); k++) {
        dp[i + 1][j] =
            max(max(dp[i + 1][j], dp[i][j]), dp[i][j - k] + b[i][k - 1]);
      }
    }
  }
  printf("%d\\n", dp[10][K]);
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, K, latte[2001] = {0};
  for (int i = 1; i <= 2000; i++)
    latte[i] = (i - 1) * i;

  cin >> N >> K;
  vector<vector<int>> books(10);
  for (int i = 0; i < N; i++) {
    int v, j;
    cin >> v >> j;
    j--;
    books[j].push_back(v);
  }

  for (int i = 0; i < 10; i++) {
    sort(books[i].begin(), books[i].end(), greater<int>());
    for (int j = 0; j < books[i].size(); j++) {
      if (j + 1 == books[i].size()) {
        books[i][j] += j * (j + 1);
        continue;
      }
      books[i][j + 1] += books[i][j];
      books[i][j] += j * (j + 1);
    }
  }

  int dp[11][2001];
  fill(dp[0], dp[0] + 11 * 2001, -1);
  for (int i = 0; i <= 10; i++)
    dp[i][0] = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= K; j++) {
      if (dp[i][j] == -1)
        break;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      for (int k = 0; k < books[i].size(); k++) {
        if (j + k + 1 > K)
          break;
        dp[i + 1][j + k + 1] =
            max(dp[i + 1][j + k + 1], dp[i][j] + books[i][k]);
      }
    }
  }
  /*
  for(int i=0;i<10;i++){
      if(!books[i].size())continue;
      cout<<i<<":";
      for(int j=0;j<books[i].size();j++)cout<<books[i][j]<<" ";
      cout<<endl;
  }
  cout<<endl<<endl;
  for(int i=0;i<=10;i++){
      for(int j=0;j<=K;j++)cout<<dp[i][j]<<" ";
      cout<<endl;
  }
  */
  cout << dp[10][K] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int N, K, latte[2001] = {0};
  for (int i = 1; i <= 2000; i++)
    latte[i] = (i - 1) * i;

  cin >> N >> K;
  vector<vector<int>> books(10);
  for (int i = 0; i < N; i++) {
    int v, j;
    cin >> v >> j;
    j--;
    books[j].push_back(v);
  }

  for (int i = 0; i < 10; i++) {
    sort(books[i].begin(), books[i].end(), greater<int>());
    for (int j = 0; j < books[i].size(); j++) {
      if (j + 1 == books[i].size()) {
        books[i][j] += j * (j + 1);
        continue;
      }
      books[i][j + 1] += books[i][j];
      books[i][j] += j * (j + 1);
    }
  }

  int dp[11][2001];
  fill(dp[0], dp[0] + 11 * 2001, -1);
  for (int i = 0; i <= N; i++)
    dp[i][0] = 0;
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j <= K; j++) {
      if (dp[i][j] == -1)
        break;
      dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      for (int k = 0; k < books[i].size(); k++) {
        if (j + k + 1 > K)
          break;
        dp[i + 1][j + k + 1] =
            max(dp[i + 1][j + k + 1], dp[i][j] + books[i][k]);
      }
    }
  }
  /*
  for(int i=0;i<10;i++){
      if(!books[i].size())continue;
      cout<<i<<":";
      for(int j=0;j<books[i].size();j++)cout<<books[i][j]<<" ";
      cout<<endl;
  }
  cout<<endl<<endl;
  for(int i=0;i<=10;i++){
      for(int j=0;j<=K;j++)cout<<dp[i][j]<<" ";
      cout<<endl;
  }
  */
  cout << dp[10][K] << endl;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

const int G = 11;
const int N = 2010;
const int K = N;

int calc(int g, int num, int val[][N]) {
  int sum = 0;
  for (int i = 0; i < num; ++i) {
    sum += val[g][i];
  }
  return sum + num * (num - 1);
}

int main() {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;

    static int val[G][N];
    static int dp[G][K];
    static int num[G] = {
        0,
    };

    for (int i = 0; i < n; ++i) {
      int v, g;
      cin >> v >> g;
      val[g][num[g]] = v;
      num[g]++;
    }

    for (int g = 0; g < G; ++g) {
      sort(val[g], val[g] + num[g]);
      reverse(val[g], val[g] + num[g]);
    }

    for (int i = 0; i < G; ++i) {
      for (int j = 0; j <= k; ++j) {
        dp[i][j] = 0;
      }
    }

    for (int g = 1; g < G; ++g) {
      for (int i = 0; i <= k; ++i) {
        for (int l = 0; l <= k; ++l) {
          if (i - l < 0)
            break;
          if (l > num[g])
            break;
          dp[g + 1][i] = max(dp[g + 1][i], dp[g][i - l] + calc(g, l, val));
        }
      }
    }

    cout << dp[G][k] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

const int G = 11;
const int N = 2010;

int calc(int g, int num, int val[][N]) {
  int sum = 0;
  for (int i = 0; i < num; ++i) {
    sum += val[g][i];
  }
  return sum + num * (num - 1);
}

int main() {
  int n, k;
  while (cin >> n >> k) {
    if (n == 0 && k == 0)
      break;

    int val[G][N];
    int dp[G][1 + k];
    int num[G] = {
        0,
    };

    for (int i = 0; i < n; ++i) {
      int v, g;
      cin >> v >> g;
      val[g][num[g]] = v;
      num[g]++;
    }

    for (int g = 0; g < G; ++g) {
      sort(val[g], val[g] + num[g]);
      reverse(val[g], val[g] + num[g]);
    }

    for (int i = 0; i < G; ++i) {
      for (int j = 0; j <= k; ++j) {
        dp[i][j] = 0;
      }
    }

    for (int g = 1; g < G; ++g) {
      for (int i = 0; i <= k; ++i) {
        for (int l = 0; l <= k; ++l) {
          if (i - l < 0)
            break;
          if (l > num[g])
            break;
          dp[g + 1][i] = max(dp[g + 1][i], dp[g][i - l] + calc(g, l, val));
        }
      }
    }

    cout << dp[G - 1][k] << endl;
  }
  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Runtime Error

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

int n, k;
vector<int> c[10];
int memo[10][5000];

int calc(int g, int b) {
  int ret = 0;

  if (b == 0)
    return 0;
  if (g == 0) {
    for (int i = 0; i < b; ++i)
      ret += c[g][i];
    return ret;
  }

  if (memo[g][b] != -1)
    return memo[g][b];

  int sum = 0;

  ret = calc(g - 1, b);

  for (int i = 1; i <= b; ++i) {

    sum += c[g][i - 1];
    ret = max(ret, sum + calc(g - 1, b - i));
  }

  return memo[g][b] = ret;
}

int main() {
  memset(memo, -1, sizeof(memo));

  scanf("%d%d", &n, &k);
  for (int i = 0; i < 10; ++i)
    c[i].reserve(n);
  for (int i = 0; i < n; ++i) {
    int tc, tg;
    scanf("%d%d", &tc, &tg);
    c[tg - 1].push_back(tc);
  }

  for (int i = 0; i < 10; ++i) {

    sort(c[i].rbegin(), c[i].rend());
    for (int j = 1; j < c[i].size(); ++j)
      c[i][j] += (2 * (j + 1) - 2);
  }

  printf("%d\\n", calc(9, k));

  return 0;
}

Error: 0

Fixed submission:

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

int n, k;
vector<int> c[10];
int memo[10][5000];

int calc(int g, int b) {
  int ret = 0;

  if (b == 0)
    return 0;
  if (g == 0) {
    for (int i = 0; i < b; ++i)
      ret += c[g][i];
    return ret;
  }

  if (memo[b][g] != -1)
    return memo[b][g];

  int sum = 0;

  ret = calc(g - 1, b);

  for (int i = 1; i <= b; ++i) {

    sum += c[g][i - 1];
    ret = max(ret, sum + calc(g - 1, b - i));
  }

  return memo[b][g] = ret;
}

int main() {
  memset(memo, -1, sizeof(memo));

  scanf("%d%d", &n, &k);
  for (int i = 0; i < 10; ++i)
    c[i].reserve(n);
  for (int i = 0; i < n; ++i) {
    int tc, tg;
    scanf("%d%d", &tc, &tg);
    c[tg - 1].push_back(tc);
  }

  for (int i = 0; i < 10; ++i) {

    sort(c[i].rbegin(), c[i].rend());
    for (int j = 1; j < c[i].size(); ++j)
      c[i][j] += (2 * (j + 1) - 2);
  }

  printf("%d\\n", calc(9, k));

  return 0;
}
Problem p00484 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int main() {
  int N, K, C, G, dp[11][2000], cost;
  vector<int> latte[10];
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> C >> G;
    latte[--G].push_back(C);
  }

  for (int i = 0; i < 11; i++)
    for (int j = 0; j <= K; j++)
      dp[i][j] = -1;
  dp[0][0] = 0;
  for (int i = 0; i < 10; i++) {
    sort(latte[i].begin(), latte[i].end(), greater<int>());
    // for(int j=0;j<latte[i].size();j++) printf("%d ",latte[i][j]); puts("");
    cost = 0;
    for (int j = 0; j <= latte[i].size(); j++) {
      if (j > 0)
        cost += latte[i][j - 1];

      for (int k = 0; k <= K; k++) {
        /*if(j==0){
                if(dp[i][k]==-1) printf("- ");
                else printf("%d ",dp[i][k]);
        }*/
        if (k + j > K)
          break;
        if (dp[i][k] == -1)
          continue;
        dp[i + 1][k + j] = max(dp[i + 1][k + j], dp[i][k] + cost + (j - 1) * j);
      } // puts("");
    }
  }
  printf("%d\\n", dp[10][K]);
  return 0;
}

Error: MLE

Fixed submission:

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

struct data {
  int books[10], val;
  data() {
    for (int i = 0; i < 10; i++)
      books[i] = 0;
    val = -1;
  }
};

data latte[2000][1 << 10];

int main() {
  int N, K, C, G;
  data latte_c;
  cin >> N >> K;
  latte[0][0].val = 0;
  for (int i = 0; i < N; i++) {
    cin >> C >> G;
    G--;
    // printf("%d:\\n",i);
    for (int j = K - 1; j >= 0; j--) {
      for (int k = (1 << 10) - 1; k >= 0; k--) {
        // if(latte[j][k].val == -1) printf("- ");
        // else printf("%d ",latte[j][k].val);
        if (latte[j][k].val == -1)
          continue;
        latte_c = latte[j][k];
        latte_c.books[G]++;
        latte_c.val += C + (latte_c.books[G] - 1) * 2;
        if (latte[j + 1][k | (1 << G)].val < latte_c.val)
          latte[j + 1][k | (1 << G)] = latte_c;
      } // puts("");
    }   // puts("");
  }
  int ans = 0;
  // printf("ans:\\n");
  for (int i = 0; i < (1 << 10); i++) {
    ans = max(ans, latte[K][i].val);
    // printf("%d ",latte[K][i].val);
  } // puts("");
  printf("%d\\n", ans);
  return 0;
}