alexjercan/bugnet huggingface dataset:
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define INF (1 << 29)

using namespace std;

int main() {
  int N, M;

  scanf("%d", &N);
  scanf("%d", &M);

  vector<int> P(N);
  vector<int> C(M);
  vector<int> E(M);

  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
  }

  for (int i = 0; i < M; i++) {
    scanf("%d", &C[i]);
    scanf("%d", &E[i]);
  }

  vector<vector<int>> dp(M + 1, vector<int>(N + 1, INF));
  dp[0][0] = 0;

  for (int i = 1; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = min(dp[i - 1][j], dp[i - 1][max(0, j - C[i - 1])] + E[i - 1]);
    }
  }

  sort(P.begin(), P.end(), greater<int>());

  vector<int> S(N + 1);
  S[0] = 0;

  for (int i = 1; i <= N; i++) {
    S[i] = S[i - 1] + P[i - 1];
  }

  int ret = 0;

  for (int i = 0; i <= N; i++) {
    ret = max(ret, S[i] - dp[M][i]);
  }

  printf("%d\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

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

#define INF (1 << 29)

using namespace std;

int main() {
  int N, M;

  scanf("%d", &N);
  scanf("%d", &M);

  vector<int> P(N);
  vector<int> C(M);
  vector<int> E(M);

  for (int i = 0; i < N; i++) {
    scanf("%d", &P[i]);
  }

  for (int i = 0; i < M; i++) {
    scanf("%d", &C[i]);
    scanf("%d", &E[i]);
  }

  vector<vector<int>> dp(M + 1, vector<int>(N + 1, INF));
  dp[0][0] = 0;

  for (int i = 1; i <= M; i++) {
    for (int j = 0; j <= N; j++) {
      dp[i][j] = dp[i - 1][j];

      for (int k = max(0, j - C[i - 1]); k < j; k++) {
        dp[i][j] = min(dp[i][j], dp[i - 1][k] + E[i - 1]);
      }
    }
  }

  sort(P.begin(), P.end(), greater<int>());

  vector<int> S(N + 1);
  S[0] = 0;

  for (int i = 1; i <= N; i++) {
    S[i] = S[i - 1] + P[i - 1];
  }

  int ret = 0;

  for (int i = 0; i <= N; i++) {
    ret = max(ret, S[i] - dp[M][i]);
  }

  printf("%d\\n", ret);

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

// メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int m, n;
  const int MAX_N = 500, MAX_M = 10000;
  int p[MAX_M];
  int c[MAX_N], e[MAX_N];
  scanf("%d%d", &m, &n);
  for (int i = 0; i < m; i++)
    scanf("%d", &p[i]);
  sort(p, p + m);
  reverse(p, p + m);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &c[i], &e[i]);

  int dp[MAX_M + 1];
  for (int i = 0; i <= MAX_M; i++)
    dp[i] = 214700000;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = m; j >= 0; j--) {
      dp[j] = min(dp[j], dp[max(j - c[i], 0)] + e[i]);
    }
  }
  int ans = 0;
  int sum = 0;
  for (int i = 0; i <= m; i++) {
    ans = max(ans, sum - dp[i]);
    if (i < m)
      sum += p[i];
  }
  printf("%d\\n", ans);
  return 0;
}
// メモリ数確認したか?

Error: -11

Fixed submission:

// メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int m, n;
  const int MAX_N = 500, MAX_M = 10000;
  int p[MAX_M];
  int c[MAX_N], e[MAX_N];
  scanf("%d%d", &m, &n);
  for (int i = 0; i < m; i++)
    scanf("%d", &p[i]);
  sort(p, p + m);
  reverse(p, p + m);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &c[i], &e[i]);

  int dp[MAX_M * MAX_N + 1];
  for (int i = 0; i <= MAX_M * MAX_N; i++)
    dp[i] = 2147000000;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = MAX_M * MAX_N - c[i]; j >= 0; j--) {
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
    }
  }
  int ans = 0;
  int sum = 0;
  for (int i = 0; i <= MAX_M * MAX_N; i++) {
    ans = max(ans, sum - dp[i]);
    if (i < m) {
      sum += p[i];
    }
  }
  printf("%d\\n", ans);
  return 0;
}
// メモリ数確認したか?
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <functional>
#include <iostream>

typedef long long ll;

int m, n;
ll p[10001], c[10001], e[10001];
ll dp[501][10001];

int main() {
  for (int i = 0; i < 501; i++) {
    for (int j = 0; j < 10001; j++)
      dp[i][j] = LLONG_MAX;
  }
  dp[0][0] = 0;

  std::cin >> m >> n;

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

  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 10000; j++) {
      if (dp[i][j] != LLONG_MAX) {
        dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j]);
        dp[i + 1][std::min<ll>(10000, j + c[i])] =
            std::min(dp[i + 1][std::min<ll>(10000, j + c[i])], dp[i][j] + e[i]);
      }
    }
  }
  for (int i = 10000 - 1; i >= 0; i--)
    dp[n][i] = std::min(dp[n][i], dp[n][i + 1]);

  std::sort(p, p + m, std::greater<ll>());

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

  ll max = 0;
  for (int i = 0; i < m; i++) {
    max = std::max(max, p[i] - dp[n][i + 1]);
  }
  std::cout << max << std::endl;

  return 0;
}

Error: -11

Fixed submission:

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

const long long max = 1000000000;

long long p[10001];
long long c[501];
long long e[501];
long long dp[501][10001];

int main() {

  int m, n;
  std::cin >> m >> n;

  for (int i = 0; i < m; i++) {
    std::cin >> p[i];
  }
  std::sort(p, p + m, std::greater<long long>());

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

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

  for (int i = 0; i <= 500; i++) {
    for (int j = 0; j <= 20000; j++) {
      dp[i][j] = max;
    }
  }
  dp[1][0] = 0;
  dp[1][c[0]] = e[0];

  for (int i = 1; i < n; i++) {
    for (int j = 0; j <= 20000; j++) {
      if (dp[i][j] != max) {
        dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j]);
        if (j + c[i] <= 20000)
          dp[i + 1][j + c[i]] = std::min(dp[i + 1][j + c[i]], dp[i][j] + e[i]);
      }
    }
  }

  for (int i = 9999; i >= 0; i--) {
    dp[n][i] = std::min(dp[n][i], dp[n][i + 1]);
  }

  long long max = 0;
  for (int i = 1; i <= m; i++) {
    max = std::max(max, p[i - 1] - dp[n][i]);
  }

  std::cout << max << std::endl;

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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define F first
#define S second
const int INF = 1 << 30, MAX_M = 10000;
typedef pair<int, int> P;

int dp[501][MAX_M + MAX_M];
int p[MAX_M + 1];
int main() {
  int n, m, ans = 0;
  vector<int> vec, vec2;
  P c[MAX_M + 1];
  for (int i = 1; i < 501; i++)
    fill(dp[i] + 1, dp[i] + MAX_M + MAX_M, INF);
  cin >> m >> n;
  for (int i = 1; i <= m; i++) {
    scanf("%d", &p[i]);
  }
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    c[i] = P(a, b);
  }
  sort(p + 1, p + m + 1, greater<int>());
  sort(c, c + n);
  for (int i = 2; i <= m; i++) {
    p[i] += p[i - 1];
  }
  vec.push_back(0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < vec2.size(); j++) {
      vec.push_back(vec2[j]);
    }
    vec2.clear();
    sort(vec.begin(), vec.end());
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    for (int j = 0; j < vec.size(); j++) {
      if (vec[j] < m) {
        dp[i + 1][c[i].F + vec[j]] =
            min(dp[i][vec[j]] + c[i].S, dp[i + 1][c[i].F + vec[j]]);
        if (dp[i][c[i].F + vec[j]])
          dp[i + 1][c[i].F + vec[j]] =
              min(dp[i + 1][c[i].F + vec[j]], dp[i][c[i].F + vec[j]]);
        dp[i + 1][vec[j]] = min(dp[i][vec[j]], dp[i + 1][vec[j]]);
        vec2.push_back(c[i].F + vec[j]);
      } else {
        if (dp[i][vec[j]])
          dp[i + 1][vec[j]] = min(dp[i + 1][vec[j]], dp[i][vec[j]]);
      }
    }
  }
  for (int j = 0; j < vec2.size(); j++) {
    vec.push_back(vec2[j]);
  }
  vec2.clear();
  sort(vec.begin(), vec.end());
  vec.erase(unique(vec.begin(), vec.end()), vec.end());
  for (int i = 0; i < vec.size(); i++) {
    int a = vec[i];
    if (vec[i] > m)
      a = m;
    ans = max(ans, p[a] - dp[n][vec[i]]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

// Solution for aoj 0599:IOI Manju
#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
int n, m, ans;
int p[501], box[10001][2];
int dp[10001][501];
int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    cin >> box[i][0] >> box[i][1];
  // sort(p, p + m, greater<int>());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (j >= box[i][0] - 1) {
        int a;
        if (i)
          a = dp[i - 1][j - box[i][0]] - box[i][1];
        else
          a = box[i][1] * -1;
        for (int k = j - box[i][0] + 1; k <= j; k++) {
          a += p[k];
          int b = max(dp[i][k - 1], dp[i - 1][k]);
          dp[i][k] = max(dp[i][k], a);
          dp[i][k] = max(dp[i][k], b);
        }
      }
    }
  }
  cout << dp[n - 1][m - 1] << endl;
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

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

const int INF = 1145141919;
int m, n, sum, ans;
int box[501][2], dp[501][10001];
vector<int> manju;

int main() {
  scanf("%d%d", &m, &n);
  for (int i = 1; i <= m; i++) {
    int v;
    scanf("%d", &v);
    manju.push_back(v);
  }
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &box[i][0], &box[i][1]);

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

  manju.push_back(INF);
  sort(manju.begin(), manju.end(), greater<int>());

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (box[i][0] >= j) {
        dp[i][j] = min(dp[i - 1][j], box[i][1]);
      } else {
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - box[i][0]] + box[i][1]);
      }
    }
  }

  for (int i = 1; i <= m; i++) {
    sum += manju[i];
    ans = max(ans, sum - dp[n][i]);
  }

  printf("%d\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

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

const int INF = 1145141919;
int m, n, p[10001], c[501], e[501], dp[2][10001], ans = 0, price = 0,
                                                  capacity = 0;
int main() {
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i] >> e[i];
  }
  for (int i = 0; i < 2; i++) {
    fill(dp[i], dp[i] + 10001, INF);
  }

  sort(p, p + m, greater<int>());

  for (int i = 0; i < n; i++) {
    capacity += c[i];
    for (int j = 0; j < capacity; j++) {
      if (c[i] > j) {
        dp[i % 2][j] = min(dp[(i % 2) ? 0 : 1][j], e[i]);
      } else if (capacity > j) {
        dp[i % 2][j] =
            min(dp[(i % 2) ? 0 : 1][j], dp[(i % 2) ? 0 : 1][j - c[i]] + e[i]);
      }
    }
  }

  for (int i = 0; i < m; i++) {
    price += p[i];
    ans = max(ans, price - dp[(n - 1) % 2][i]);
  }

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

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 30)
int hako[2][10001];
int main() {
  int m, n;
  int p[10001], c, e;
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m, greater<int>());
  fill_n(*hako, 2 * 10001, INF);
  for (int i = 1; i <= n; i++) {
    cin >> c >> e;
    for (int j = 1; j <= m; j++) {
      if (j <= c)
        hako[1][j] = min(hako[0][j], e);
      else
        hako[1][j] = min(hako[0][j], hako[0][j - c] + e);
    }
    for (int j = 1; j <= m && i != n; j++) {
      hako[0][j] = hako[1][j];
      hako[1][j] = INF;
    }
  }
  int ma = 0, manju = 0;
  for (int i = 1; i <= m; i++) {
    if (hako[1][i] == INF)
      continue;
    manju += p[i - 1];
    ma = max(ma, manju - hako[1][i]);
  }
  cout << ma << endl;
  return (0);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF (1 << 30)
int hako[10001][10001];
int main() {
  int m, n;
  int p[10001], c[10001], e[10001];
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> p[i];
  sort(p, p + m, greater<int>());
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> e[i];
  fill_n(*hako, 10001 * 10001, INF);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (j <= c[i])
        hako[i][j] = min(hako[i - 1][j], e[i]);
      else
        hako[i][j] = min(hako[i - 1][j], hako[i - 1][j - c[i]] + e[i]);
    }
  }
  int ma = 0, manju = 0;
  for (int i = 1; i <= m; i++) {
    if (hako[n][i] == INF)
      continue;
    manju += p[i - 1];
    ma = max(ma, manju - hako[n][i]);
  }
  cout << ma << endl;
  return (0);
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int m, n;
  cin >> m >> n;
  vector<int> p(m), c(n), e(n);
  rep(i, 0, m) cin >> p[i];
  rep(i, 0, n) cin >> c[i] >> e[i];

  sort(p.rbegin(), p.rend());
  vector<int> sum(m + 1);
  rep(i, 0, m) sum[i + 1] = sum[i] + p[i];

  int ans = max(0, sum[min(c[0], m)] - e[0]);
  static int dp[500][10001];
  fill_n((int *)dp, 500 * 10001, INF);
  rep(i, 0, c[0] + 1) dp[0][i] = e[0];
  rep(i, 1, n) {
    rep(j, 0, c[i] + 1) dp[i][j] = e[i];
    rep(j, 0, m + 1) {
      dp[i][j] =
          min(dp[i][j], min(dp[i - 1][j], dp[i - 1][max(0, j - c[i])] + e[i]));
      ans = max(ans, sum[j] - dp[i][j]);
    }
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

void Solve() {
  int m, n;
  cin >> m >> n;
  vector<int> p(m), c(n), e(n);
  rep(i, 0, m) cin >> p[i];
  rep(i, 0, n) cin >> c[i] >> e[i];

  sort(p.rbegin(), p.rend());
  vector<int> sum(m + 1);
  rep(i, 0, m) sum[i + 1] = sum[i] + p[i];

  int ans = max(0, sum[c[0]] - e[0]);
  static int dp[500][10001];
  fill_n((int *)dp, 500 * 10001, INF);
  rep(i, 0, c[0] + 1) dp[0][i] = e[0];
  rep(i, 1, n) {
    rep(j, 0, c[i] + 1) dp[i][j] = e[i];
    rep(j, 0, m + 1) {
      dp[i][j] =
          min(dp[i][j], min(dp[i - 1][j], dp[i - 1][max(0, j - c[i])] + e[i]));
      ans = max(ans, sum[j] - dp[i][j]);
    }
  }

  cout << ans << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  int n, m;
  cin >> n >> m;
  vi in(n);
  rep(i, n) cin >> in[i];
  rep(i, n) in[i] *= -1;
  sort(all(in));
  vi dp(10010, inf);
  dp[0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    for (int j = 10009; j >= 0; j--) {
      dp[j] = min(dp[j], dp[max(0, j - a)] + b);
    }
  }
  int out = 0;
  int sum = 0;
  rep(i, n) {
    sum -= in[i];
    out = max(out, sum - dp[i + 1]);
  }
  cout << out << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
int main() {
  int n, m;
  cin >> n >> m;
  vi in(n);
  rep(i, n) cin >> in[i];
  rep(i, n) in[i] *= -1;
  sort(all(in));
  vi dp(10010, inf);
  dp[0] = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    for (int j = 10010; j >= 0; j--) {
      dp[j] = min(dp[j], dp[max(0, j - a)] + b);
    }
  }
  int out = 0;
  int sum = 0;
  rep(i, n) {
    sum -= in[i];
    out = max(out, sum - dp[i + 1]);
  }
  cout << out << endl;
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dp[100100];
int main() {
  int m, n;
  cin >> m >> n;
  int c[550], e[550];
  vector<int> p(m);
  REP(i, 100100) dp[i] = INF;
  REP(i, m) cin >> p[i];
  dp[0] = 0;
  int sum = 0;
  REP(i, n) {
    cin >> c[i] >> e[i];
    for (int j = sum; j >= 0; j--) {
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
    }
    sum += c[i];
    sum = min(sum, 20000);
  }
  for (int i = sum; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1]);
  }
  sort(ALL(p), greater<int>());
  sum = 0;
  int ans = 0;
  REP(i, m) {
    sum += p[i];
    ans = max(ans, sum - dp[i + 1]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1e9;
int dp[100100];
int main() {
  int m, n;
  cin >> m >> n;
  int c[550], e[550];
  vector<int> p(m);
  REP(i, 100100) dp[i] = INF;
  REP(i, m) cin >> p[i];
  dp[0] = 0;
  int sum = 0;
  REP(i, n) {
    cin >> c[i] >> e[i];
    for (int j = sum; j >= 0; j--) {
      dp[j + c[i]] = min(dp[j + c[i]], dp[j] + e[i]);
    }
    sum += c[i];
    sum = min(sum, 100000);
  }
  for (int i = sum; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1]);
  }
  sort(ALL(p), greater<int>());
  sum = 0;
  int ans = 0;
  REP(i, m) {
    sum += p[i];
    ans = max(ans, sum - dp[i + 1]);
  }
  cout << ans << endl;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

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

struct hg {
  int l;
  int m;
};

int m, n;
vector<int> man;
vector<hg> hako;
int memo[30000];

int dp(int h, int ma) {
  if (h >= n)
    return 0;
  memo[ma] = min(memo[max(0, ma - hako[h].l)] + hako[h].m, memo[ma]);
  if (ma == 0)
    return 0;
  else
    dp(h, ma - 1);
  return 0;
}

int main() {
  cin >> m >> n;
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    man.push_back(x);
  }
  for (int i = 0; i < 30000; i++)
    memo[i] = 9999999999999;
  sort(man.rbegin(), man.rend());
  for (int i = 1; i < m; i++) {
    man[i] += man[i - 1];
  }
  for (int i = 0; i < n; i++) {
    hg o;
    cin >> o.l >> o.m;
    hako.push_back(o);
  }
  int ans = 0;
  memo[0] = 0;
  for (int i = 0; i < n; i++)
    dp(i, m);
  for (int i = 1; i <= m; i++) {
    ans = max(-1 * (memo[i] - man[i - 1]), ans);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

struct hg {
  int l;
  int m;
};

int m, n;
vector<int> man;
vector<hg> hako;
int memo[30000];

int check(int w, int i) {
  int ans = 0;
  if (w - 1 < 0)
    ans = man[w + i - 1];
  else
    ans = man[w + i - 1] - man[w - 1];
  return ans;
}
int dp(int h, int ma) {
  if (h >= n)
    return 0;
  memo[ma] = min(memo[max(0, ma - hako[h].l)] + hako[h].m, memo[ma]);
  if (ma == 0)
    dp(h + 1, m);
  else
    dp(h, ma - 1);
  return 0;
}

int main() {
  cin >> m >> n;
  memset(memo, -1, sizeof(memo));
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    man.push_back(x);
  }
  for (int i = 0; i < 30000; i++)
    memo[i] = 9999999999999;
  sort(man.rbegin(), man.rend());
  for (int i = 1; i < m; i++) {
    man[i] += man[i - 1];
  }
  for (int i = 0; i < n; i++) {
    hg o;
    cin >> o.l >> o.m;
    hako.push_back(o);
  }
  int ans = 0;
  memo[0] = 0;
  dp(0, m);
  for (int i = 1; i <= m; i++) {
    ans = max(-1 * (memo[i] - man[i - 1]), ans);
  }
  cout << ans << endl;
}