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

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

const int64_t INF = 1e+8;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> hp(n);
    REP(i, n) cin >> hp[i];
    int m;
    cin >> m;
    vector<tuple<int, int>> sg;
    vector<tuple<int, int>> al;
    REP(i, m) {
      string name, tg;
      int mp, dm;
      cin >> name >> mp >> tg >> dm;
      if (tg == "Single")
        sg.emplace_back(mp, dm);
      else
        al.emplace_back(mp, dm);
    }
    int a = sg.size();
    int b = al.size();
    vector<vector<int>> dps(a + 1, vector<int>(100001, INF));
    vector<vector<int>> dpa(b + 1, vector<int>(100001, INF));
    dps[0][0] = 0;
    dpa[0][0] = 0;
    REP(i, a) {
      REP(j, 100001) {
        dps[i + 1][j] = dps[i][j];
        if (j >= get<1>(sg[i]))
          dps[i + 1][j] =
              min(dps[i + 1][j], dps[i + 1][j - get<1>(sg[i])] + get<0>(sg[i]));
        else
          dps[i + 1][j] = min(dps[i + 1][j], get<0>(sg[i]));
      }
    }
    REP(i, b) {
      REP(j, 100001) {
        dpa[i + 1][j] = dpa[i][j];
        if (j >= get<1>(al[i]))
          dpa[i + 1][j] =
              min(dpa[i + 1][j], dpa[i + 1][j - get<1>(al[i])] + get<0>(al[i]));
        else
          dpa[i + 1][j] = min(dpa[i + 1][j], get<0>(al[i]));
      }
    }
    vector<int64_t> costs(100001);
    REP(i, n) {
      REP(j, hp[i]) { costs[j] += dps[a][hp[i] - j]; }
    }
    int64_t mcos = INF;
    REP(i, 100001) mcos = min(mcos, dpa[b][i] + costs[i]);
    cout << mcos << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

const int64_t INF = 1e+8;

int main() {
  while (1) {
    int n;
    cin >> n;
    if (!n)
      break;
    vector<int> hp(n);
    REP(i, n) cin >> hp[i];
    int m;
    cin >> m;
    vector<tuple<int64_t, int64_t>> sg;
    vector<tuple<int64_t, int64_t>> al;
    REP(i, m) {
      string name, tg;
      int mp, dm;
      cin >> name >> mp >> tg >> dm;
      if (tg == "Single")
        sg.emplace_back(mp, dm);
      else
        al.emplace_back(mp, dm);
    }
    int a = sg.size();
    int b = al.size();
    vector<vector<int64_t>> dps(a + 1, vector<int64_t>(100001, INF));
    vector<vector<int64_t>> dpa(b + 1, vector<int64_t>(100001, INF));
    dps[0][0] = 0;
    dpa[0][0] = 0;
    REP(i, a) {
      REP(j, 100001) {
        dps[i + 1][j] = dps[i][j];
        if (j >= get<1>(sg[i]))
          dps[i + 1][j] =
              min(dps[i + 1][j], dps[i + 1][j - get<1>(sg[i])] + get<0>(sg[i]));
        else
          dps[i + 1][j] = min(dps[i + 1][j], get<0>(sg[i]));
      }
    }
    REP(i, b) {
      REP(j, 100001) {
        dpa[i + 1][j] = dpa[i][j];
        if (j >= get<1>(al[i]))
          dpa[i + 1][j] =
              min(dpa[i + 1][j], dpa[i + 1][j - get<1>(al[i])] + get<0>(al[i]));
        else
          dpa[i + 1][j] = min(dpa[i + 1][j], get<0>(al[i]));
      }
    }
    vector<int64_t> costs(100001);
    REP(i, n) {
      REP(j, hp[i]) { costs[j] += dps[a][hp[i] - j]; }
    }
    int64_t mcos = INF;
    REP(i, 100001) mcos = min(mcos, dpa[b][i] + costs[i]);
    cout << mcos << endl;
  }
  return 0;
}