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

#include <bits/stdc++.h>
#define N 100
#define D 100001
#define INF (1e9)
using namespace std;
typedef pair<int, int> P;

int n, hp[N], m, mp[N], damage[N], ans, cnt;
int dpAll[D], dpSin[D];
string name, target[N];
vector<P> v;
vector<int> minv;

int bynary_search(int x) {
  int L = 0, R = v.size();
  while (L < R) {
    int M = (L + R) / 2;
    if (v[M].first >= x)
      R = M;
    else
      L = M + 1;
  }
  return L;
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> hp[i];
    cin >> m;
    for (int i = 0; i < m; i++)
      cin >> name >> mp[i] >> target[i] >> damage[i];
    for (int i = 0; i < D; i++)
      dpAll[i] = dpSin[i] = INF;
    dpAll[0] = dpSin[0] = 0;
    for (int i = 0; i < D; i++) {
      if (dpAll[i] == INF)
        continue;
      for (int j = 0; j < m; j++) {
        if (target[j] != "All")
          continue;
        if (i + damage[j] < D)
          dpAll[i + damage[j]] = min(dpAll[i + damage[j]], dpAll[i] + mp[j]);
        else
          dpAll[D - 1] = min(dpAll[D - 1], dpAll[i] + mp[j]);
      }
    }
    for (int i = 0; i < D; i++) {
      if (dpSin[i] == INF)
        continue;
      for (int j = 0; j < m; j++) {
        if (target[j] != "Single")
          continue;
        if (i + damage[j] < D)
          dpSin[i + damage[j]] = min(dpSin[i + damage[j]], dpSin[i] + mp[j]);
        else
          dpSin[D - 1] = min(dpSin[D - 1], dpSin[i] + mp[j]);
      }
    }
    for (int i = 0; i < D; i++)
      if (dpSin[i] != INF)
        v.push_back(P(i, dpSin[i])), minv.push_back(0);
    int a = INF;
    for (int i = (int)v.size() - 1; i >= 0; i--) {
      a = min(a, v[i].second);
      minv[i] = a;
    }
    ans = INF;
    for (int i = 0; i < D; i++) {
      if (dpAll[i] == INF)
        continue;
      cnt = dpAll[i];
      for (int j = 0; j < n; j++) {
        int k = hp[j] - i;
        cnt += minv[bynary_search(k)];
      }
      ans = min(ans, cnt);
    }
    cout << ans << endl;
    v.clear();
    minv.clear();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define N 100
#define INF (1e9)
#define HP 100001
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1; // mpcost,N,HP,Alldamage
typedef pair<int, P> P2;

int n, hp[N], m, mp[N], damage[N];
string name, target[N];
map<P2, int> pam;

int dijkstra() {
  int r;
  priority_queue<P1, vector<P1>, greater<P1>> q;
  q.push(P1(P(0, 0), P(0, 0)));
  pam[P2(0, P(0, 0))] = 0;
  while (!q.empty()) {
    P1 t = q.top();
    q.pop();
    int mpcost = t.first.first;
    int y = t.first.second;
    int x = t.second.first;
    int alldamage = t.second.second;
    int d;
    if (pam.find(P2(y, P(x, alldamage))) == pam.end())
      d = INF;
    else
      d = pam[P2(y, P(x, alldamage))];
    if (d < mpcost)
      continue;
    if (y == n - 1 && x >= hp[y]) {
      r = mpcost;
      break;
    }
    if (x >= hp[y]) {
      if (pam.find(P2(y + 1, P(alldamage, alldamage))) == pam.end())
        d = INF;
      else
        d = pam[P2(y + 1, P(alldamage, alldamage))];
      if (d > mpcost) {
        pam[P2(y + 1, P(alldamage, alldamage))] = mpcost;
        q.push(P1(P(mpcost, y + 1), P(alldamage, alldamage)));
      }
      continue;
    }
    for (int i = 0; i <= m; i++) {
      int nmpcost = mpcost + mp[i];
      int ny = y, nx = x + damage[i];
      int nalldamage = alldamage;
      if (target[i] == "All")
        nalldamage += damage[i];
      if (nx > hp[ny])
        nx = hp[ny];
      if (pam.find(P2(ny, P(nx, nalldamage))) == pam.end())
        d = INF;
      else
        d = pam[P2(ny, P(nx, nalldamage))];
      if (d > nmpcost) {
        pam[P2(ny, P(nx, nalldamage))] = nmpcost;
        q.push(P1(P(nmpcost, ny), P(nx, nalldamage)));
      }
    }
  }
  return r;
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> hp[i];
    cin >> m;
    for (int i = 0; i < m; i++)
      cin >> name >> mp[i] >> target[i] >> damage[i];
    cout << dijkstra() << endl;
    pam.clear();
  }
  return 0;
}