Problem p01274 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define for_rev(i, a, b) for (int i = (a); i >= (b); --i)

#define MAX_DM 1000000

int N, M, HP[111];
vector<int> singleMP, allMP;
vector<int> singleDM, allDM;

int singleDP[MAX_DM], allDP[MAX_DM];
const int INF = (int)1e9;

int main() {
  while (cin >> N, N) {
    for_(i, 0, N) cin >> HP[i];

    string dumy, type;
    cin >> M;

    singleMP.clear();
    allMP.clear();
    singleDM.clear();
    allDM.clear();

    for_(i, 0, M) {
      int MP, DM;
      cin >> dumy >> MP >> type >> DM;

      DM = min(DM, 100000);
      if (DM == 0)
        continue;

      if (type == "All") {
        allMP.push_back(MP);
        allDM.push_back(DM);
      } else {
        singleMP.push_back(MP);
        singleDM.push_back(DM);
      }
    }

    // single DP
    int S = singleMP.size();
    fill(singleDP, singleDP + MAX_DM, INF);
    singleDP[0] = 0;

    for_(i, 0, S) {
      int DM = singleDM[i], MP = singleMP[i];

      for_(j, DM, MAX_DM) {
        singleDP[j] = min(singleDP[j], singleDP[j - DM] + MP);
      }
    }

    vector<int> smp(MAX_DM, singleDP[MAX_DM - 1]);
    for_rev(d, MAX_DM - 2, 0) smp[d] = min(smp[d + 1], singleDP[d]);

    // all DP
    int A = allMP.size();
    fill(allDP, allDP + MAX_DM, INF);
    allDP[0] = 0;

    for_(i, 0, A) {
      int DM = allDM[i], MP = allMP[i];

      for_(j, DM, MAX_DM) { allDP[j] = min(allDP[j], allDP[j - DM] + MP); }
    }

    vector<int> amp(MAX_DM, allDP[MAX_DM - 1]);
    for_rev(d, MAX_DM - 2, 0) amp[d] = min(amp[d + 1], allDP[d]);

    int ans = INF;

    for_(d, 0, MAX_DM) {
      int sum = amp[d];

      for_(i, 0, N) if (HP[i] - d > 0) {
        sum += smp[HP[i] - d];
        sum = min(sum, INF);
      }

      ans = min(ans, sum);
    }

    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

#define for_(i, a, b) for (int i = (a); i < (b); ++i)
#define for_rev(i, a, b) for (int i = (a); i >= (b); --i)

#define MAX_DM 5000000

int N, M, HP[111];
vector<int> singleMP, allMP;
vector<int> singleDM, allDM;

int singleDP[MAX_DM], allDP[MAX_DM];
const int INF = (int)1e9;

int main() {
  cin >> N;
  for_(i, 0, N) cin >> HP[i];

  string dumy, type;
  cin >> M;

  for_(i, 0, M) {
    int MP, DM;
    cin >> dumy >> MP >> type >> DM;

    if (type == "All") {
      allMP.push_back(MP);
      allDM.push_back(DM);
    } else {
      singleMP.push_back(MP);
      singleDM.push_back(DM);
    }
  }

  // single DP
  int S = singleMP.size();
  fill(singleDP, singleDP + MAX_DM, INF);
  singleDP[0] = 0;

  for_(i, 0, S) {
    int DM = singleDM[i], MP = singleMP[i];

    for_(j, DM, MAX_DM) {
      singleDP[j] = min(singleDP[j], singleDP[j - DM] + MP);
    }
  }

  vector<int> smp(MAX_DM, singleDP[MAX_DM - 1]);
  for_rev(d, MAX_DM - 2, 0) smp[d] = min(smp[d + 1], singleDP[d]);

  // all DP
  int A = allMP.size();
  fill(allDP, allDP + MAX_DM, INF);
  allDP[0] = 0;

  for_(i, 0, A) {
    int DM = allDM[i], MP = allMP[i];

    for_(j, DM, MAX_DM) { allDP[j] = min(allDP[j], allDP[j - DM] + MP); }
  }

  vector<int> amp(MAX_DM, allDP[MAX_DM - 1]);
  for_rev(d, MAX_DM - 2, 0) amp[d] = min(amp[d + 1], allDP[d]);

  int ans = INF;

  for_(d, 0, MAX_DM) {
    int sum = amp[d];
    for_(i, 0, N) if (HP[i] - d > 0) sum += smp[HP[i] - d];
    ans = min(ans, sum);
  }

  cout << ans << endl;
}