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

#include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF (999999999 / 2)

int main() {
  int m, n;
  cin >> m >> n;
  vector<int> man;
  rep(i, m) {
    int a;
    cin >> a;
    man.pb(a);
  }
  vector<pii> box;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    box.pb(pii(a, b));
  }
  int dp[30010];
  rep(i, 30010) dp[i] = INF;
  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 30000; j >= 0; j--) {
      if (dp[j - box[i].first] != INF && j - box[i].first >= 0) {
        dp[j] = min(dp[j], dp[j - box[i].first] + box[i].second);
      }
    }
  }
  vector<int> data;
  rep(i, 30000) data.pb(dp[i]);

  for (int i = 30000 - 1; i >= 1; i--) {
    //        cout<<data[i]<<endl;
    if (data[i] < data[i - 1])
      data[i - 1] = data[i];
  }
  sort(all(man), greater<int>());

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

  int ans = 0;
  rep(i, m) { ans = max(ans, man[i] - data[i + 1]); }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;

#define dump(x) cerr << #x << " = " << (x) << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

int main() {
  int m, n;
  cin >> m >> n;
  vector<int> man;
  rep(i, m) {
    int a;
    cin >> a;
    man.pb(a);
  }
  vector<pii> box;
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    box.pb(pii(a, b));
  }

  static int dp[600][30000];
  rep(i, 30000) rep(j, 600) dp[j][i] = INF;
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = n; j >= 1; j--) {
      for (int k = 0; k <= 30000; k++) {
        if (k - box[i].first >= 0)
          dp[j][k] = min(dp[j - 1][k - box[i].first] + box[i].second, dp[j][k]);
      }
    }
  }
  vector<int> data;
  rep(i, 30000) {
    int x = INF;
    rep(j, n + 1) {
      x = min(x, dp[j][i]);
      //            cout<<"dp["<<j<<"]["<<i<<"]="<<dp[j][i]<<endl;
    }
    data.pb(x);
  }
  for (int i = 30000 - 1; i >= 1; i--) {
    if (data[i] < data[i - 1])
      data[i - 1] = data[i];
  }
  //    rep(i,data.size())cout<<data[i]<<" ";cout<<endl;
  //    rep(i,10)cout<<data[i]<<" ";
  sort(all(man), greater<int>());
  for (int i = 1; i < man.size(); i++) {
    man[i] = man[i - 1] + man[i];
  }
  int ans = 0;
  rep(i, m) {
    ans = max(ans, man[i] - data[i + 1]);
    //        cout<<i<<" "<<ans<<" "<<data[i+1]<<endl;
  }
  cout << ans << endl;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int kos[550], ned[550], man[11000];
  int data[10050];
  cin >> m >> n;
  for (int i = 0; i < m; i++)
    cin >> man[i];
  for (int i = 0; i < n; i++)
    cin >> kos[i] >> ned[i];
  sort(man, man + m);
  reverse(man, man + m);
  for (int i = 0; i <= m; i++)
    data[i] = 110110110;
  data[0] = 0;
  for (int j = 0; j < n; j++) {
    for (int i = m; i >= 0; --i) {
      data[i] = min(data[max(i - kos[j], 0)] + ned[j], data[i]);
    }
  }
  // for(int i=0;i<=m;i++)cout<<data[i]<<endl;
  int jo = 0;
  for (int i = 0; i < m; i++) {
    jo += man[i];
    data[i + 1] -= jo;
  }

  int ans = 0;
  for (int i = 0; i <= m; i++)
    ans = min(data[i], ans);

  cout << abs(ans) << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int zan[500] = {};
struct HAKO {
  int i;
  int n;
  int cosp;
};
int main() {
  int seb = 0;
  int man[100000];
  HAKO h[5000];
  int numman, numhako;
  cin >> numman >> numhako;
  // in
  for (int i = 0; i < numman; i++) {
    cin >> man[i];
  }
  for (int i = 0; i < numhako; i++) {
    cin >> h[i].i >> h[i].n;
    h[i].cosp = h[i].n / h[i].i;
    zan[i] = h[i].i;
    seb += h[i].i;
  }
  // sort
  for (int i = 0; i < numhako; i++) {
    int g = i;
    for (int k = i; k < numhako; k++) {
      if (h[g].cosp > h[k].cosp) {
        g = k;
      }
    }
    if (i != g)
      swap(h[i], h[g]);
  }
  sort(man, man + numman);
  reverse(man, man + numman);
  int data[10000] = {};
  for (int i = 0; i < numman; i++) {
    (i == 0) ? data[i] += man[i] : data[i] = data[i - 1] + man[i];
  }
  int now = 0;
  int se = 0;
  se += h[now].n;
  for (int i = 0; i < numman; i++) {
    data[i] -= se;
    h[now].i--;
    if (h[now].i == 0) {
      now++;
      se += h[now].n;
    }
  }

  int ans = 0;
  for (int i = 0; i < seb; i++) {
    ans = max(ans, data[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int dp[512][10005];
int N, M;
int P[10005], C[512], E[512];

int dfs(int n, int m) {
  if (n >= N)
    return P[min(m, M)];
  int &res = dp[n][m];
  if (res >= 0)
    return res;
  return res = max(dfs(n + 1, m), dfs(n + 1, min(m + C[n], M)) - E[n]);
}

int main() {
  scanf("%d%d", &M, &N);
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= M; i++)
    scanf("%d", &P[i]);
  sort(P + 1, P + M + 1, greater<int>());
  for (int i = 1; i < M; i++)
    P[i + 1] += P[i];
  for (int i = 0; i < N; i++)
    scanf("%d%d", &C[i], &E[i]);

  printf("%d\\n", dfs(0, 0));
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

const int INF = 1 << 29;
int dp[512][10005];
int N, M;
int P[10005], C[512], E[512];

int dfs(int n, int m) {
  if (n == N)
    return P[min(m, M)];
  int res = -INF;
  res = max(res, dfs(n + 1, m + C[n]) - E[n]);
  res = max(res, dfs(n + 1, m));
  return res;
}

int main() {
  scanf("%d%d", &M, &N);
  memset(dp, -1, sizeof dp);
  for (int i = 1; i <= M; i++)
    scanf("%d", &P[i]);
  sort(P + 1, P + M + 1, greater<int>());
  for (int i = 1; i < M; i++)
    P[i + 1] += P[i];
  for (int i = 0; i < N; i++)
    scanf("%d%d", &C[i], &E[i]);

  printf("%d\\n", dfs(0, 0));
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define rep(x, y) for (int x = 0; x < (y); x++)

typedef long long ll;

int M, N;
int P[10005], C[512], E[512];
ll dp[10005][512];

ll dfs(int m, int n) {
  if (m > M || n >= N)
    return 0;
  ll &res = dp[m][n];
  if (res >= 0)
    return res;
  res = 0;
  if (m <= M)
    res = max(res,
              dfs(min(M, m + C[n]), n + 1) + P[min(M, m + C[n])] - P[m] - E[n]);
  res = max(res, dfs(m, n + 1));
  return res;
}

int main() {
  scanf("%d%d", &M, &N);
  P[0] = 0;
  rep(i, M) scanf("%d", P + i + 1);
  sort(P + 1, P + M + 1, greater<int>());
  rep(i, M) P[i + 1] += P[i];
  rep(i, N) scanf("%d%d", C + i, E + i);
  memset(dp, -1, sizeof(dp));
  printf("%lld\\n", dfs(0, 0));
}

Error: TLE

Fixed submission:

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

using namespace std;

#define rep(x, y) for (int x = 0; x < (y); x++)

typedef long long ll;

int M, N;
int P[10005], C[512], E[512];
ll dp[10005][512];

ll dfs(int m, int n) {
  if (m > M || n >= N)
    return 0;
  ll res = 0;
  if (m <= M)
    res = max(res,
              dfs(min(M, m + C[n]), n + 1) + P[min(M, m + C[n])] - P[m] - E[n]);
  res = max(res, dfs(m, n + 1));
  return res;
}

int main() {
  scanf("%d%d", &M, &N);
  P[0] = 0;
  rep(i, M) scanf("%d", P + i + 1);
  sort(P + 1, P + M + 1, greater<int>());
  rep(i, M) P[i + 1] += P[i];
  rep(i, N) scanf("%d%d", C + i, E + i);
  memset(dp, -1, sizeof(dp));
  printf("%lld\\n", dfs(0, 0));
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int M, N, P[20000], C[500], E[500];
int dp[500][20000];

int dfs(int n, int m) {
  if (n == N) {
    return P[m];
  }
  if (dp[n][m] >= 0)
    return dp[n][m];
  return dp[n][m] = max(dfs(n + 1, m),
                        dfs(n + 1, (m + C[n]) > M ? M : (m + C[n])) - E[n]);
}

int main() {
  scanf("%d%d", &M, &N);
  memset(dp, -1, sizeof(dp));

  rep(i, M) scanf("%d", P + i + 1);
  sort(P + 1, P + M + 1, greater<int>());
  rep(i, 19999) P[i + 1] += P[i];

  rep(i, N) scanf("%d%d", C + i, E + i);

  printf("%d\\n", dfs(0, 0));
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int M, N, P[20000], C[500], E[500];
int dp[500][100000];

int dfs(int n, int m) {
  if (n == N) {
    return P[m];
  }
  if (dp[n][m] >= 0)
    return dp[n][m];
  return dp[n][m] = max(dfs(n + 1, m), dfs(n + 1, m + C[n]) - E[n]);
}

int main() {
  scanf("%d%d", &M, &N);
  memset(dp, -1, sizeof(dp));

  rep(i, M) scanf("%d", P + i + 1);
  sort(P + 1, P + M + 1, greater<int>());
  rep(i, 19999) P[i + 1] += P[i];

  rep(i, N) scanf("%d%d", C + i, E + i);

  printf("%d\\n", dfs(0, 0));
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

// int dy[]={0, 0, 1, -1, 0};
// int dx[]={1, -1, 0, 0, 0};

int main() {
  int n, m;
  scanf("%d%d", &m, &n);
  vector<int> a(m);
  vector<P> b(n);
  REP(i, m) scanf("%d", &a[i]);
  VSORT(a);
  reverse(a.begin(), a.end());
  REP(i, n) scanf("%d%d", &b[i].first, &b[i].second);

  vector<vector<int>> dp(n + 10, vector<int>(m + 10, -INF));
  dp[0][0] = 0;
  FOR(i, 1, n + 1) {
    REP(j, m + 1) {
      int tmp = max(j - b[i - 1].first, 0);
      if (dp[i - 1][tmp] == -INF)
        continue;
      dp[i][j] =
          max(max(dp[i][j], dp[i - 1][tmp] - b[i - 1].second), dp[i - 1][j]);
    }
  }

  int sum = 0, ans = 0;
  FOR(i, 1, m + 1) {
    sum += a[i - 1];
    ans = max(ans, dp[n][i] + sum);
  }

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

// int dy[]={0, 0, 1, -1, 0};
// int dx[]={1, -1, 0, 0, 0};

int main() {
  int n, m;
  scanf("%d%d", &m, &n);
  vector<int> a(m);
  vector<P> b(n);
  REP(i, m) scanf("%d", &a[i]);
  VSORT(a);
  reverse(a.begin(), a.end());
  REP(i, n) scanf("%d%d", &b[i].first, &b[i].second);

  vector<vector<int>> dp(n + 10, vector<int>(m + 10, -INF));
  dp[0][0] = 0;
  REP(i, n) {
    REP(j, m + 1) {
      if (dp[i][j] != -INF) {
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
        int tmp = min(j + b[i].first + 1, m + 1);
        FOR(k, j + 1, tmp) {
          dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] - b[i].second);
        }
      }
    }
  }

  int sum = 0, ans = 0;
  FOR(i, 1, m + 1) {
    sum += a[i - 1];
    ans = max(ans, dp[n][i] + sum);
  }

  printf("%d\\n", ans);
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
#define INF 1000000000

int man[10010], kaz[510], ned[510], dp[2][10010];
int main() {
  int M, N, sum, a, b, cnt, hog;
  cin >> M >> N;
  for (int i = 0; i < M; i++)
    cin >> man[i];
  for (int i = 0; i < N; i++)
    cin >> kaz[i] >> ned[i];
  for (int i = 0; i <= M; i++)
    dp[0][i] = INF;
  dp[0][0] = 0;
  sort(man, man + M, greater<int>());

  for (int i = 0; i < N; i++) {
    a = i % 2;
    b = (i + 1) % 2;
    for (int j = 0; j <= M; j++) {
      dp[b][j] = dp[a][j];
    }
    for (int j = 0; j < M; j++) {
      hog = min(j + kaz[i], M);
      dp[b][hog] = min(dp[b][hog], dp[a][j] + ned[i]);
    }
  }
  b = N % 2;
  sum = cnt = 0;
  for (int i = 1; i <= M; i++) {
    sum += man[i - 1];
    cnt = max(cnt, sum - dp[b][i]);
    // cout<<i<<" "<<sum<<" "<<dp[b][i]<<endl;//
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
using namespace std;
#define INF 1000000000

int man[10010], kaz[510], ned[510], dp[2][10010];
int main() {
  int M, N, sum, a, b, cnt;
  cin >> M >> N;
  for (int i = 0; i < M; i++)
    cin >> man[i];
  for (int i = 0; i < N; i++)
    cin >> kaz[i] >> ned[i];
  for (int i = 0; i <= M; i++)
    dp[0][i] = INF;
  dp[0][0] = 0;
  sort(man, man + M, greater<int>());

  for (int i = 0; i < N; i++) {
    a = i % 2;
    b = (i + 1) % 2;
    for (int j = 0; j <= M; j++) {
      dp[b][j] = dp[a][j];
    }
    for (int j = 0; j <= M; j++) {
      for (int k = j + 1; k <= j + kaz[i]; k++) {
        if (k <= M)
          dp[b][k] = min(dp[b][k], dp[a][j] + ned[i]);
      }
    }
  }
  b = N % 2;
  sum = cnt = 0;
  for (int i = 1; i <= M; i++) {
    sum += man[i - 1];
    cnt = max(cnt, sum - dp[b][i]);
    // cout<<i<<" "<<sum<<" "<<dp[b][i]<<endl;//
  }
  cout << cnt << endl;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int main() {
  int M, N, P[10000], C[500], E[500];
  cin >> M >> N;
  for (int i = 0; i < M; i++) {
    cin >> P[i];
  }
  sort(P, P + M, greater<int>());

  for (int i = 0; i < N; i++) {
    cin >> C[i] >> E[i];
  }

  int dp[50001]; // dp[i]:饅頭をi個以上入れるための箱の値段の最小値
  fill_n(dp, 50001, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = M; j >= 0; j--) {
      if (dp[j] != INF) {
        dp[min(M, j + C[i])] = min(dp[min(M, j + C[i])], dp[j] + E[i]);
      }
    }
  }

  int sum[10001];
  sum[1] = P[0];
  for (int i = 2; i <= M; i++) {
    sum[i] = sum[i - 1] + P[i - 1];
  }

  int ret = 0;
  for (int i = 1; i <= M; i++) {
    if (dp[i] != INF)
      ret = max(ret, sum[i] - dp[i]);
  }
  cout << ret << endl;
  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;

int main() {
  int M, N, P[10000], C[100], E[100];
  cin >> M >> N;
  for (int i = 0; i < M; i++) {
    cin >> P[i];
  }
  sort(P, P + M, greater<int>());

  for (int i = 0; i < N; i++) {
    cin >> C[i] >> E[i];
  }

  int dp[50001]; // dp[i]:饅頭をi個以上入れるための箱の値段の最小値
  fill_n(dp, 50001, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = M; j >= 0; j--) {
      if (dp[j] != INF) {
        dp[min(M, j + C[i])] = min(dp[min(M, j + C[i])], dp[j] + E[i]);
      }
    }
  }

  int sum[10001];
  sum[1] = P[0];
  for (int i = 2; i <= M; i++) {
    sum[i] = sum[i - 1] + P[i - 1];
  }

  int ret = 0;
  for (int i = 1; i <= M; i++) {
    if (dp[i] != INF)
      ret = max(ret, sum[i] - dp[i]);
  }
  cout << ret << endl;
  return (0);
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>

using namespace std;
int M, N;
int P[10001];
int C[501];
int E[501];
int MaxM = 0;
int TP[10001] = {};
const int INF = 200000000;
int dp[10001][501] = {};

int calc(int a, int b) {
  int ans = INF;
  if (b < 0)
    return ans;
  if (a <= 0)
    return 0;
  if (dp[a][b] > -1)
    return dp[a][b];
  ans = calc(a, b - 1);
  if (a <= C[b]) {
    ans = min(ans, E[b]);
  } else {
    ans = min(ans, calc(a - C[b], b - 1) + E[b]);
  }

  dp[a][b] = ans;
  return ans;
}
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];

  sort(P, P + M);
  TP[1] = P[M - 1];
  for (int i = 2; i < M + 1; ++i) {
    TP[i] = TP[i - 1] + P[M - i];
  }

  for (int i = 0; i < N; ++i)
    MaxM += C[i];
  MaxM = min(MaxM, M);

  fill(dp[0], dp[10001], -1);
  int R = 0;
  for (int i = 1; i < MaxM + 1; ++i) {
    R = max(R, TP[i] - calc(i, N - 1));
  }
  cout << R << endl;
  return 0;
}

Error: TLE

Fixed submission:

//============================================================================
// Name        : JOI.cpp
// Author      :2015 ho3
// Version     :???11:08
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <iostream>

using namespace std;
int M, N;
int P[10001];
int C[501];
int E[501];
int MaxM = 0;
int TP[10001] = {};
const int INF = 200000000;
int dp[10001][501] = {};

int calc(int a, int b) {
  int ans = INF;
  if (b < 0)
    return ans;
  if (dp[a][b] < INF)
    return dp[a][b];
  ans = calc(a, b - 1);
  if (a <= C[b]) {
    ans = min(ans, E[b]);
  } else {
    ans = min(ans, calc(a - C[b], b - 1) + E[b]);
  }

  dp[a][b] = ans;
  return ans;
}
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];

  sort(P, P + M);
  TP[1] = P[M - 1];
  for (int i = 2; i < M + 1; ++i) {
    TP[i] = TP[i - 1] + P[M - i];
  }

  for (int i = 0; i < N; ++i)
    MaxM += C[i];
  MaxM = min(MaxM, M);

  fill(dp[0], dp[10001], INF);
  int R = 0;
  for (int i = 1; i < MaxM + 1; ++i) {
    R = max(R, TP[i] - calc(i, N));
  }
  cout << R << endl;
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
#define INF 1234567890

int manju[123456];
int before_manju[123456];

int manju_num;
int box_num;

// ??±?????\\????\\????
int can_box[12345];

// ??±?????????
int box_exp[500];

int memo[12345][1000];
int answer;

// x = manju
// y = box

int dp(int x, int y) {
  if (x <= 0)
    return 0;
  if (memo[x][y] >= 0)
    return memo[x][y];
  if (y == 0)
    return INF;
  else {
    return memo[x][y] = min(dp(x, y - 1),
                            dp(x - can_box[y - 1], y - 1) + box_exp[y - 1]);
  }
}

int main() {
  int buf = 0;
  memset(memo, 255, sizeof(memo));
  scanf("%d%d", &manju_num, &box_num);
  for (int a = 0; a < manju_num; a++) {
    scanf("%d", &before_manju[a]);
  }
  for (int a = 0; a < box_num; a++) {
    scanf("%d%d", &can_box[a], &box_exp[a]);
  }
  sort(before_manju, before_manju + manju_num);
  reverse_copy(before_manju, before_manju + manju_num, manju);
  for (int a = 1; a <= manju_num; a++) {
    buf += manju[a - 1];
    answer = max(answer, buf - dp(a, box_num));
  }
  printf("%d\\n", answer);
  return 0;
}

Error: 0

Fixed submission:

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

int manju[12345];
int before_manju[12345];

int manju_num;
int box_num;

// ??±?????\\????\\????
int can_box[1000];

// ??±?????????
int box_exp[1000];

int dp(int num, int now_box_money, int now_box) {
  if (num == manju_num) {
    int ans = 0;
    for (int a = 0; a < now_box; a++) {
      ans += manju[a];
    }
    return ans - now_box_money;
  } else {
    return max(
        dp(num + 1, now_box_money + box_exp[num], now_box + can_box[num]),
        dp(num + 1, now_box_money, now_box));
  }
}

int main() {
  scanf("%d%d", &manju_num, &box_num);
  for (int a = 0; a < manju_num; a++) {
    scanf("%d", &before_manju[a]);
  }
  for (int a = 0; a < box_num; a++) {
    scanf("%d%d", &can_box[a], &box_exp[a]);
  }
  sort(before_manju, before_manju + manju_num);
  reverse_copy(before_manju, before_manju + manju_num, manju);
  printf("%d\\n", dp(0, 0, 0));
  return 0;
}
Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

// AOJ 0599
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)

const int MAX_M = 10000, MAX_N = 500, INF = 1 << 29;

int M, N;
int P[MAX_M];
int C[MAX_N], E[MAX_N];
int dp[MAX_N + 1][MAX_M + 1];

int main() {
  scanf("%d%d", &M, &N);
  rep(i, M) scanf("%d", P + i);
  rep(i, N) scanf("%d%d", C + i, E + i);

  std::sort(P, P + M, std::greater<int>());

  rep(i, N + 1) rep(j, M + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) rep(j, M + 1) {
    dp[i + 1][j] = std::min(dp[i + 1][j], dp[i][j]);

    dp[i + 1][std::min(M, j + C[i])] =
        std::min(dp[i + 1][std::min(M, j + C[i])], dp[i][j] + E[i]);
  }

  int ans = 0, sum = 0;
  rep(j, M + 1) {
    ans = std::max(ans, sum - dp[N][j]);

    if (j < M)
      sum += P[j];
  }

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

  return 0;
}

Error: TLE

Fixed submission:

// AOJ 0599
#include <algorithm>
#include <cstdio>
#include <functional>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); ++i)

const int MAX_M = 10000, MAX_N = 500, INF = 1 << 29;

int M, N;
int P[MAX_M];
int C[MAX_N], E[MAX_N];
int dp[MAX_N + 1][MAX_M + 1];

int main() {
  scanf("%d%d", &M, &N);
  rep(i, M) scanf("%d", P + i);
  rep(i, N) scanf("%d%d", C + i, E + i);

  std::sort(P, P + M, std::greater<int>());

  rep(i, N + 1) rep(j, M + 1) dp[i][j] = INF;
  dp[0][0] = 0;
  rep(i, N) rep(j, M + 1) rep(k, C[i] + 1) if (j + k <= M) dp[i + 1][j + k] =
      std::min(dp[i + 1][j + k], dp[i][j] + (k ? E[i] : 0));

  int ans = 0, sum = 0;
  rep(j, M + 1) {
    ans = std::max(ans, sum - dp[N][j]);

    if (j < M)
      sum += P[j];
  }

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

  return 0;
}