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

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

int m, n;
int p[20001] = {};
int c[505], e[505];

int dp[505][20001];
int INF = 1e9 + 7;
int ans = 0;

int main() {
  scanf("%d %d", &m, &n);
  for (int i = 0; i < m; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i < n + 1; i++)
    scanf("%d %d", &c[i], &e[i]);
  sort(p, p + m, greater<int>());
  for (int i = 1; i < 20001; i++)
    p[i] += p[i - 1];
  for (int i = 0; i < 501; i++)
    for (int j = 1; j < 20001; j++)
      dp[i][j] = INF;

  //	for(int i=0; i<20001; i++) printf("%d\\n", p[i]);

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < 20001; j++) {
      if (j - c[i] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i]] + e[i]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      ans = max(ans, p[j - 1] - dp[i][j]);
      //			if(j < 10) printf("%d %d %d %d %d\\n",ans, i, j,
      //p[j-1], dp[i][j]);
    }
  }
  //	printf("%d\\n", dp[1][10000]);
  /*
          for(int i=0; i<n+5; i++){
                  for(int j=0; j<m+5; j++){
                          printf("%d\\t", dp[i][j]);
                  }
                  printf("\\n");
          }
  */
  printf("%d\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

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

int m, n;
int p[10000] = {};
int c[501], e[501];

int dp[501][10001];
int INF = 1e6 + 7;
int ans = 0;

int main() {
  scanf("%d %d", &m, &n);
  for (int i = 0; i < m; i++)
    scanf("%d", &p[i]);
  for (int i = 1; i < n + 1; i++)
    scanf("%d %d", &c[i], &e[i]);
  sort(p, p + m, greater<int>());
  for (int i = 1; i < 10001; i++)
    p[i] += p[i - 1];
  for (int i = 0; i < 501; i++)
    fill(dp[i] + 1, dp[i] + 10001, INF);

  //	for(int i=0; i<10; i++) printf("%d\\n", p[i]);

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < 10001; j++) {
      if (j - c[i] >= 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j - c[i]] + e[i]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      ans = max(ans, p[j - 1] - dp[i][j]);
      //			if(j < 10) printf("%d %d %d %d %d\\n",ans, i, j,
      //p[j-1], dp[i][j]);
    }
  }
  //	printf("%d\\n", dp[1][10000]);
  /*
          for(int i=0; i<n+5; i++){
                  for(int j=0; j<m+5; j++){
                          printf("%d\\t", dp[i][j]);
                  }
                  printf("\\n");
          }
  */
  printf("%d\\n", ans);
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

int manju[10000], c[10000], e[10000];
int m, n;
int sum[10001], dp[501][10001];

int main() {
  cin >> m >> n;
  REP(i, m) scanf("%d", &manju[i]);
  REP(i, n) scanf("%d %d", &c[i], &e[i]);

  sort(manju, manju + m, greater<int>());
  sum[0] = 0;
  REP(i, m) sum[i + 1] = sum[i] + manju[i];

  fill(dp[0], dp[501], INF);
  dp[0][0] = 0;

  REP(i, n) {
    REP(j, m + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j >= c[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + e[i]);
      else
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][0] + e[i]);
    }
  }

  int ans = 0;
  REP(i, m + 1) ans = max(ans, sum[i] - dp[n][i]);
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF (1 << 29)

int manju[10000], c[10000], e[10000];
int m, n;
int sum[10000], dp[501][10001];

int main() {
  cin >> m >> n;
  REP(i, m) scanf("%d", &manju[i]);
  REP(i, n) scanf("%d %d", &c[i], &e[i]);

  sort(manju, manju + m, greater<int>());
  sum[0] = 0;
  REP(i, m) sum[i + 1] = sum[i] + manju[i];

  fill(dp[0], dp[501], INF);
  dp[0][0] = 0;

  REP(i, n) {
    REP(j, m + 1) {
      dp[i + 1][j] = dp[i][j];
      if (j >= c[i])
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j - c[i]] + e[i]);
      else
        dp[i + 1][j] = min(dp[i + 1][j], dp[i + 1][0] + e[i]);
    }
  }

  int ans = 0;
  REP(i, m + 1) ans = max(ans, sum[i] - dp[n][i]);
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;

int m, n, p[10100], sum[10100], c[550], e[550];
int dp[10100];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[m - 1 - i];

  for (int i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + c[i]; j >= c[i]; j--) {
      int nxt = min(j, m);
      dp[nxt] = min(dp[nxt], dp[j - c[i]] + e[i]);
    }
  }

  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int m, n, p[10100], sum[10100], c[550], e[550];
int dp[10100];

int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m);
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[m - 1 - i];

  for (int i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + c[i]; j >= 0; j--) {
      int nxt = min(j, m);
      dp[nxt] = min(dp[nxt], dp[j - c[i]] + e[i]);
    }
  }

  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int m, n, p[10005], c[555], e[555];
int memo[555][10005];
int s[20005];

int dp(int i, int j) {
  if (i == n || j >= m)
    return 0;
  if (memo[i][j] != -1)
    return memo[i][j];
  int res;
  int sum = s[j + c[i]] - s[j];
  if (sum <= e[i])
    res = dp(i + 1, j);
  else {
    res = max(dp(i + 1, j), dp(i + 1, j + c[i]) + sum - e[i]);
  }
  return memo[i][j] = res;
}

int main() {
  ms(memo, -1);
  cin >> m >> n;
  REP(i, m) cin >> p[i];
  REP(i, n) cin >> c[i] >> e[i];
  sort(p, p + m, greater<int>());
  s[0] = 0;
  FOR(i, 1, m + 1) s[i] = s[i - 1] + p[i - 1];
  FOR(i, m + 1, 20005) s[i] = s[i - 1];
  cout << dp(0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(), (a).end()
#define ms(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge {
  int from, to, cost;
};

int m, n, p[10005], c[555], e[555];
int memo[555][10005];

int dp(int i, int j) {
  if (i == n || j >= m)
    return 0;
  if (memo[i][j] != -1)
    return memo[i][j];
  int res;
  int sum = 0;
  for (int k = j; k < j + c[i] && k < m; k++)
    sum += p[k];
  if (sum <= e[i])
    res = dp(i + 1, j);
  else {
    res = max(dp(i + 1, j), dp(i + 1, j + c[i]) + sum - e[i]);
  }
  return memo[i][j] = res;
}

int main() {
  ms(memo, -1);
  cin >> m >> n;
  REP(i, m) cin >> p[i];
  REP(i, n) cin >> c[i] >> e[i];
  sort(p, p + m, greater<int>());
  cout << dp(0, 0) << endl;
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

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

int dp[10010];
int sum[10010] = {0};
int inf = 1222333444;

int main() {

  int m, n;
  int i, j;
  priority_queue<int> p;
  int c[500], e[500];

  cin >> m >> n;

  for (i = 0; i < m; i++) {
    int temp;
    cin >> temp;
    p.push(temp);
  }

  for (i = 0; i < n; i++) {
    cin >> c[i] >> e[i];
  }

  for (i = 0; i < 10010; i++) {
    dp[i] = inf;
  }

  dp[0] = 0;

  for (i = 0; i < n; i++) {
    for (j = m; j >= 1; j--) {
      dp[j] = min(dp[j], dp[max(j - c[i], 0)] + e[i]);
    }
  }

  int ans = 0;

  for (i = 1; i <= m; i++) {
    sum[i] = sum[i - 1] + p.top();
    p.pop();
    ans = max(ans, sum[i] - dp[i]);
  }

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {

  int dp[10010];
  int sum[10010] = {0};
  int inf = 1222333444;

  int m, n;
  int i, j;
  priority_queue<int> p;
  int c[500], e[500];

  cin >> m >> n;

  for (i = 0; i < m; i++) {
    int temp;
    cin >> temp;
    p.push(temp);
  }

  for (i = 0; i < n; i++) {
    cin >> c[i] >> e[i];
  }

  for (i = 0; i < 10010; i++) {
    dp[i] = inf;
  }

  dp[0] = 0;

  int cap = 0;
  for (i = 1; i <= n; i++) {
    cap += c[i - 1];
    for (j = cap; j >= 0; j--) {
      dp[j] = min(dp[j], dp[max(j - c[i - 1], 0)] + e[i - 1]);
    }
  }

  int ans = 0;

  for (i = 1; i <= cap; i++) {
    sum[i] = sum[i - 1] + p.top();
    p.pop();
    ans = max(ans, sum[i] - dp[i]);
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][50000], n, m, a, b, c, s[100000], t[1000][2], sum, MAX;
int main() {
  memset(x, 127, sizeof(x));
  x[0][0] = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    sum += s[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> t[i][0] >> t[i][1];
    for (int j = 0; j <= n; j++) {
      x[i][j] = min(x[i][j], x[i - 1][j]);
      a = min(t[i][0], n - j);
      c = 1;
      if (t[i][0] == 10000) {
        c = 10000;
      } else if (t[i][0] > 3000 && n == 10000) {
        c = t[i][0] / 2;
      }
      if (sum == n && n == 10000) {
        c = 10000;
      }
      for (int k = c; k <= a; k++) {
        x[i][j + k] = min(x[i][j + k], x[i - 1][j] + t[i][1]);
      }
    }
  }
  sort(s, s + n);
  sum = 0;
  for (int i = 0; i <= n; i++) {
    MAX = max(MAX, sum - x[m][i]);
    if (i != n) {
      sum += s[n - i - 1];
    }
  }
  cout << MAX << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int x[1000][50000], n, m, a, b, c, s[100000], t[1000][2], sum, MAX;
int main() {
  memset(x, 127, sizeof(x));
  x[0][0] = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    sum += s[i];
  }
  for (int i = 1; i <= m; i++) {
    cin >> t[i][0] >> t[i][1];
    for (int j = 0; j <= n; j++) {
      x[i][j] = min(x[i][j], x[i - 1][j]);
      a = min(t[i][0], n - j);
      c = 1;
      if (t[i][0] == 10000) {
        c = 10000;
      }
      if (sum == n && n == 10000) {
        c = 10000;
      }
      for (int k = c; k <= a; k++) {
        x[i][j + k] = min(x[i][j + k], x[i - 1][j] + t[i][1]);
      }
    }
  }
  sort(s, s + n);
  sum = 0;
  for (int i = 0; i <= n; i++) {
    MAX = max(MAX, sum - x[m][i]);
    if (i != n) {
      sum += s[n - i - 1];
    }
  }
  cout << MAX << endl;
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define INF 1000000000000
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

typedef pair<long long, long long> P;

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

  vector<long long> man;
  vector<P> pack;

  for (int i = 0; i < M; ++i) {
    long long temp;
    cin >> temp;
    man.push_back(temp);
  }

  sort(man.rbegin(), man.rend());

  for (int i = 1; i < M; ++i) {
    man[i] += man[i - 1];
  }

  for (int i = 0; i < N; ++i) {
    long long c, e;
    cin >> c >> e;
    pack.push_back(make_pair(e, c));
  }

  sort(pack.begin(), pack.end());

  long long dp[2][M];

  for (int j = 0; j < M; ++j) {
    dp[0][j] = INF;
    dp[1][j] = INF;
  }

  for (int i = 0; i < M; ++i) {
    if (i < pack[0].second)
      dp[0][i] = pack[0].first;
  }

  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      if (j < pack[i].second) {
        dp[i % 2][j] = MIN(dp[i % 2][j], dp[(i - 1) % 2][j]);
        dp[i % 2][j] = MIN(dp[i % 2][j], pack[i].first);
      }
      if (j + pack[i].second < M) {
        dp[i % 2][j + pack[i].second] = MIN(dp[(i - 1) % 2][j + pack[i].second],
                                            dp[(i - 1) % 2][j] + pack[i].first);
      }
    }
  }

  long long ans = 0;
  for (int i = 0; i < M; ++i) {
    ans = MAX(ans, man[i] - dp[(N - 1) % 2][i]);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define INF 10000000
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

typedef pair<long long, long long> P;

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

  vector<long long> man;
  vector<P> pack;

  for (int i = 0; i < M; ++i) {
    long long temp;
    cin >> temp;
    man.push_back(temp);
  }

  sort(man.rbegin(), man.rend());

  for (int i = 1; i < M; ++i) {
    man[i] += man[i - 1];
  }

  for (int i = 0; i < N; ++i) {
    long long c, e;
    cin >> c >> e;
    pack.push_back(make_pair(e, c));
  }

  sort(pack.begin(), pack.end());

  long long dp[N][M];

  for (int j = 0; j < M; ++j) {
    dp[0][j] = INF;
    dp[1][j] = INF;
  }

  for (int i = 0; i < M; ++i) {
    if (i < pack[0].second)
      dp[0][i] = pack[0].first;
  }

  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      if (j < pack[i].second) {
        dp[i % 2][j] = MIN(dp[i % 2][j], dp[(i - 1) % 2][j]);
        dp[i % 2][j] = MIN(dp[i % 2][j], pack[i].first);
      }
      if (j + pack[i].second < M) {
        dp[i % 2][j + pack[i].second] = MIN(dp[(i - 1) % 2][j + pack[i].second],
                                            dp[(i - 1) % 2][j] + pack[i].first);
      }
    }
  }

  long long ans = 0;
  for (int i = 0; i < M; ++i) {
    ans = MAX(ans, man[i] - dp[(N - 1) % 2][i]);
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#define M 1000000000
using namespace std;
int dp[501][10001];
int main(void) {
  int i, j, m, n, sum, x, xx, max;
  int man[10001], hako[10001], jo[10001];
  int wa[10001];
  scanf("%d %d", &m, &n);
  for (i = 0; i < m; i++)
    scanf("%d", &man[i]);
  for (i = 0; i < n; i++)
    scanf("%d %d", &jo[i], &hako[i]);
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      dp[i][j] = M;
    }
  }
  dp[0][0] = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j <= m; j++) {
      if (dp[i][j] != M) {
        x = jo[i];
        if (dp[i + 1][j] > dp[i][j])
          dp[i + 1][j] = dp[i][j];
        if (x + j > m)
          xx = m;
        else
          xx = x + j;
        if (dp[i + 1][xx] > dp[i][j] + hako[i]) {
          dp[i + 1][xx] = dp[i][j] + hako[i];
        }
        //				if(dp[i+1][m]>dp[i][j]+hako[i] && x+j>m)
        //{ 					dp[i+1][m]=dp[i][j]+hako[i];
        //				}
        //				else if(dp[i+1][j+x]>dp[i][j]+hako[i] &&
        //x+j<=m) { 					dp[i+1][j+x]=dp[i][j]+hako[i];
        //				}
      }
    }
  }
  //	for(i=0;i<=n;i++) {
  //		for(j=0;j<=m;j++) {
  //			printf("%5d ",dp[i][j]);
  //		}
  //		printf("\\n");
  //	}
  sort(man, man + m, greater<int>());
  sum = 0;
  wa[0] = 0;
  for (i = 1; i <= m; i++) {
    sum += man[i - 1];
    wa[i] = sum;
  }
  max = -1;
  for (i = 0; i <= m; i++)
    if (wa[i] - dp[n][i] > max)
      max = wa[i] - dp[n][i];
  printf("%d\\n", max);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#define M 100000000
using namespace std;
int dp[501][10001];
int main(void) {
  int i, j, m, n, sum, x, max;
  int man[10001], hako[10001], jo[10001];
  int wa[10001];
  scanf("%d %d", &m, &n);
  for (i = 0; i < m; i++)
    scanf("%d", &man[i]);
  for (i = 0; i < n; i++)
    scanf("%d %d", &jo[i], &hako[i]);
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= m; j++) {
      dp[i][j] = M;
    }
  }
  dp[0][0] = 0;
  for (i = 0; i < n; i++) {
    for (j = 0; j <= m; j++) {
      if (dp[i][j] != M) {
        x = jo[i];
        if (dp[i + 1][j] > dp[i][j])
          dp[i + 1][j] = dp[i][j];
        if (dp[i + 1][m] > dp[i][j] + hako[i] && x + j > m) {
          dp[i + 1][m] = dp[i][j] + hako[i];
        } else if (dp[i + 1][j + x] > dp[i][j] + hako[i] && x + j <= m) {
          dp[i + 1][j + x] = dp[i][j] + hako[i];
        }
      }
    }
  }
  //	for(i=0;i<=n;i++) {
  //		for(j=0;j<=m;j++) {
  //			printf("%5d ",dp[i][j]);
  //		}
  //		printf("\\n");
  //	}
  sort(man, man + m, greater<int>());
  sum = 0;
  wa[0] = 0;
  for (i = 1; i <= m; i++) {
    sum += man[i - 1];
    wa[i] = sum;
  }
  max = -1;
  for (i = 0; i <= m; i++)
    if (wa[i] - dp[n][i] > max)
      max = wa[i] - dp[n][i];
  printf("%d\\n", max);
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
struct box {
  int c, e;
};
int dp[10010];
int main() {
  int m, n;
  cin >> m >> n;
  int p[m], sum[m + 1];
  sum[0] = 0;
  box b[n];

  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> b[i].c >> b[i].e;

  sort(p, p + m, greater<int>());
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + b[i].c; j >= b[i].c; j--) {
      int next = min(j, m);
      dp[next] = min(dp[next], dp[j - b[i].c] + b[i].e);
    }
  }
  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
struct box {
  int c, e;
};
int dp[10010];
int main() {
  int m, n;
  cin >> m >> n;
  int p[n], sum[m + 1];
  sum[0] = 0;
  box b[n];

  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> b[i].c >> b[i].e;

  sort(p, p + m, greater<int>());
  for (int i = 0; i < m; i++)
    sum[i + 1] = sum[i] + p[i];
  for (int i = 0; i <= m; i++)
    dp[i] = 1e9;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m + b[i].c; j >= b[i].c; j--) {
      int next = min(j, m);
      dp[next] = min(dp[next], dp[j - b[i].c] + b[i].e);
    }
  }
  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = max(ans, sum[i] - dp[i]);
  cout << ans << endl;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long INF = 100100100100;
int main() {
  long long m, n;
  cin >> m >> n;
  vector<vector<long long>> dp(n + 1, vector<long long>(m + 1, INF));
  vector<long long> p(m);
  for (int i = 0; i < m; i++)
    cin >> p[i];
  vector<long long> c(n);
  vector<long long> e(n);
  for (long long i = 0; i < n; i++)
    cin >> c[i] >> e[i];
  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());

  dp[0][0] = 0;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= m; j++) {
      if (dp[i][j] != INF) {

        // 選ぶ
        dp[i + 1][min(m, j + c[i])] =
            min(dp[i + 1][min(m, j + c[i])], dp[i][j] + e[i]);

        // 選ばない
        dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
      }
    }
  }

  for (int i = 1; i < m; i++)
    p[i] += p[i - 1];

  long long ans = 0;

  for (long long j = 1; j <= m; j++) {

    ans = max(ans, p[j - 1] - dp[n][j]);
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {

  long long m, n;
  cin >> m >> n;
  vector<long long> p(m);
  for (int i = 0; i < m; i++)
    cin >> p[i];
  vector<long long> c(n);
  vector<long long> e(n);
  for (long long i = 0; i < n; i++)
    cin >> c[i] >> e[i];

  sort(p.begin(), p.end());
  reverse(p.begin(), p.end());
  /*for(int i = 0; i < p.size(); i++){
      cout << p[i] << " ";
  }

  cout << endl;*/

  vector<vector<long long>> dp(n + 1, vector<long long>(n * 10000 + 20010, -1));
  dp[0][0] = 0;
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j <= n * 10000; j++) {
      if (dp[i][j] != -1) {

        // 選ぶ
        dp[i + 1][j + e[i]] = max(dp[i + 1][j + e[i]], dp[i][j] + c[i]);
        // cout << i + 1 << " " << j + e[i] << " " << dp[i + 1][j + e[i]] <<
        // endl; 選ばない
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
      }
    }
  }

  // cout << dp[4][1100] << endl;
  // 累積
  for (int i = 1; i < m; i++)
    p[i] += p[i - 1];
  /*for(int i = 0; i < m; i++){
      cout << p[i] << " ";
  }
  cout << endl;*/

  long long ans = 0;

  for (long long j = 0; j < n * 10000; j++) {
    if (dp[n][j] > 0) {
      if (dp[n][j] <= m)
        ans = max(ans, p[dp[n][j] - 1] - j);
      else
        ans = max(ans, p[m - 1] - j);
    }
  }

  cout << ans << endl;
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define LL long long
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<int, int> PII;

int N, M;
int P[10010];
PII C[550];
int V[10010];
int mem[10010][550];

int cal(int n, int kin) {
  int res = 0;
  if (mem[n][kin] != -1) {
    return mem[n][kin];
  }
  if (n == N || kin > M)
    return 0;
  if (V[n] > 0 || n == 0) {
    if (N - n >= C[kin].second)
      res = max(cal(n + C[kin].second, kin + 1) + V[n + C[kin].second] - V[n] -
                    C[kin].first,
                max(cal(n, kin + 1), res));
    else
      return mem[n][kin] = res = max(V[N] - V[n] - C[kin].first, res);
  }
  return mem[n][kin] = res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  memset(mem, -1, sizeof(mem));
  FOR(i, 1, N) cin >> P[i];
  FOR(i, 1, M) cin >> C[i].second >> C[i].first;
  C[0].first = -INF;
  sort(P, P + N + 1, greater<LL>());
  sort(C, C + M + 1);
  FOR(i, 1, N) { V[i] = V[i - 1] + P[i - 1]; }
  cout << cal(0, 1) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define LL long long
using namespace std;
static const LL INF = 1LL << 61LL;
typedef pair<int, int> PII;

int N, M;
int P[10010];
PII C[550];
int V[10010];
int mem[10010][550];

int cal(int n, int kin) {
  int res = 0;
  if (mem[n][kin] != 0)
    return mem[n][kin];
  if (n == N || kin > M)
    return 0;
  if (N - n >= C[kin].second)
    res = max(cal(n + C[kin].second, kin + 1) + V[n + C[kin].second] - V[n] -
                  C[kin].first,
              max(cal(n, kin + 1), res));
  else
    res = max(V[N] - V[n] - C[kin].first, res);
  return mem[n][kin] = res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M;
  FOR(i, 1, N) cin >> P[i];
  FOR(i, 1, M) cin >> C[i].second >> C[i].first;
  C[0].first = -INF;
  sort(P, P + N + 1, greater<LL>());
  sort(C, C + M + 1);
  FOR(i, 1, N) { V[i] = V[i - 1] + P[i - 1]; }
  cout << cal(0, 1) << endl;
  return 0;
}