Problem p00522 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
typedef long long ll;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " ["        \\
        << __LINE__ << "]" << endl)
struct DUMP : ostringstream {
  template <class T> DUMP &operator,(const T &t) {
    if (this->tellp())
      *this << ", ";
    *this << t;
    return *this;
  }
};
#else
#define dump(...)
#endif

typedef vector<int> vi;

int main() {
  int M, N;
  while (cin >> M >> N && M) {
    vi P(M), C(N), E(N);
    rep(i, M) cin >> P[i];
    rep(i, N) cin >> C[i] >> E[i];
    sort(all(P));
    P.insert(P.begin(), 0);
    vi sum(M + 1);
    rep(i, M) sum[i + 1] = sum[i] + P[i + 1];
    static int dp[555][11111];
    memset(dp, 0, sizeof(dp));
    rep(j, N) {
      rep(i, M) {
        dp[j + 1][i] = max(dp[j + 1][i], dp[j][i]);
        int x = min<int>(i + C[j], M);
        dp[j + 1][x] = max(dp[j + 1][x], dp[j][i] + sum[x] - sum[i] - E[j]);
      }
    }
    int ans = 0;
    rep(i, 555) rep(j, 11111) ans = max(ans, dp[i][j]);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define all(c) (c).begin(), (c).end()
#define loop(i, a, b) for (ll i = a; i < ll(b); i++)
#define rep(i, b) loop(i, 0, b)
#define mems(arr, v) memset(arr, v, sizeof(arr))
#ifdef DEBUG
#define dump(...)                                                              \\
  (cerr << #__VA_ARGS__ << " = " << (DUMP(), __VA_ARGS__).str() << " ["        \\
        << __LINE__ << "]" << endl)
struct DUMP : ostringstream {
  template <class T> DUMP &operator,(const T &t) {
    if (this->tellp())
      *this << ", ";
    *this << t;
    return *this;
  }
};
#else
#define dump(...)
#endif

int P[10011], C[511], E[511];
int dp[500 * 10000 + 10];

const int inf = 0x7F7F7F7F;
int main() {
  int M, N;
  while (cin >> M >> N && M) {
    rep(i, M) cin >> P[i];
    rep(i, N) cin >> C[i] >> E[i];
    mems(dp, 0x7F);
    dp[0] = 0;
    rep(i, N) {
      for (int j = 500 * 10000 + 10 - 1; j >= C[i]; j--) {
        dp[j] = min(dp[j], dp[j - C[i]] + E[i]);
      }
    }
    // cap, cost
    vector<pair<int, int>> bs;
    rep(i, 500 * 10000 + 10) {
      if (dp[i] != inf)
        bs.push_back(make_pair(i, dp[i]));
    }
    sort(P, P + M, greater<int>());
    // num -> max cost
    dp[0] = 0;
    rep(i, M) { dp[i + 1] = dp[i] + P[i]; }
    int ans = 0;
    rep(i, bs.size()) {
      rep(j, M + 1) {
        if (bs[i].first >= j) {
          ans = max(ans, dp[j] - bs[i].second);
        }
      }
    }
    cout << ans << endl;
  }
}