Problem p01274 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef struct Mag {
  int MP, tag, Dam; // single:0 all:1
  bool operator<(const Mag &A) const {
    return tag == A.tag ? A.MP > MP : tag < A.tag;
  }
} Mag;

int N, M, HP[105], A, SDP[100005], ADP[100005], res, tmp;
Mag SP[105];

int main() {
  for (; scanf("%d", &N), N;) {
    res = 1000000000;
    A = 0;
    memset(SDP, 0x7f, sizeof(SDP));
    memset(ADP, 0x7f, sizeof(ADP));
    SDP[0] = ADP[0] = 0;
    for (int i = 0; i < N; i++)
      scanf("%d", HP + i);
    scanf("%d\\n", &M);
    for (int i = 0; i < M; i++) {
      char t[20];
      scanf("%*s%d%s%d\\n", &SP[i].MP, t, &SP[i].Dam);
      A += SP[i].tag = t[0] == 'S' ? 0 : 1;
    }
    sort(SP, SP + M);
    for (int i = 0; i < M - A; i++) {
      for (int j = 0; j <= 100000; j++) {
        int np = j + SP[i].Dam;
        if (np > 100000)
          np = 100000;
        SDP[np] = min(SDP[np], SDP[j] + SP[i].MP);
      }
    }
    for (int i = 100000; i >= 0; i--)
      SDP[i] = min(SDP[i], SDP[i + 1]);

    for (int i = M - A; i < M; i++) {
      for (int j = 0; j <= 100000; j++) {
        int np = j + SP[i].Dam;
        if (np > 100000)
          np = 100000;
        ADP[np] = min(ADP[np], ADP[j] + SP[i].MP);
      }
    }
    if (A == M) {
      int mh = 0;
      for (int i = 0; i < N; i++)
        mh = max(mh, HP[i]);
      for (int i = mh; i <= 100000; i++)
        res = min(res, ADP[i]);
    } else {
      for (int i = 0; i <= 100000; i++) {
        if (ADP[i] == 2139062143)
          continue;
        tmp = ADP[i];
        for (int k = 0; k < N; k++) {
          if (HP[k] - i >= 0) {
            int np = HP[k] - i;
            if (np < 0)
              np = 0;
            if (SDP[np] == 2139062143) {
              tmp = 1000000000;
              break;
            }
            tmp += SDP[np];
          }
        }
        res = min(res, tmp);
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef struct Mag {
  int MP, tag, Dam; // single:0 all:1
  bool operator<(const Mag &A) const {
    return tag == A.tag ? A.MP > MP : tag < A.tag;
  }
} Mag;

typedef struct DD {
  int mp;
  vector<int> M;
  bool operator<(const DD &a) const { return mp > a.mp; };
} DD;

int N, M, HP[105], A, SDP[1599999], res;
Mag SP[105];

int main() {
  for (; scanf("%d", &N), N;) {
    res = 1000000000;
    A = 0;
    memset(SDP, 0x7f, sizeof(SDP));
    SDP[0] = 0;
    for (int i = 0; i < N; i++)
      scanf("%d", HP + i);
    scanf("%d\\n", &M);
    for (int i = 0; i < M; i++) {
      char t[20];
      scanf("%*s%d%s%d\\n", &SP[i].MP, t, &SP[i].Dam);
      A += SP[i].tag = t[0] == 'S' ? 0 : 1;
    }
    sort(SP, SP + M);
    for (int i = 0; i < M - A; i++) {
      for (int j = 0; j + SP[i].Dam < 1100000; j++)
        SDP[j + SP[i].Dam] = min(SDP[j + SP[i].Dam], SDP[j] + SP[i].MP);
    }
    for (int i = 1100000 - 1; i >= 0; i--)
      SDP[i] = min(SDP[i], SDP[i + 1]);
    priority_queue<DD> Q;
    DD tmp;
    tmp.mp = 0;
    for (int i = 0; i < N; i++)
      tmp.M.push_back(HP[i]);
    Q.push(tmp);
    for (; !Q.empty();) {
      tmp = Q.top();
      Q.pop();
      int cs = 0, f = 0;
      if (tmp.mp >= res)
        continue;
      for (int i = 0; i < N; i++) {
        if (tmp.M[i] > 0) {
          cs += SDP[tmp.M[i]];
          f = 1;
        }
      }
      res = min(res, tmp.mp + cs);
      if (f) {
        for (int i = M - A; i < M; i++) {
          if (SP[i].Dam == 0)
            continue;
          DD ins = tmp;
          for (int j = 0; j < N; j++) {
            if (ins.M[j] > 0)
              ins.M[j] -= SP[i].Dam;
          }
          ins.mp += SP[i].MP;
          Q.push(ins);
        }
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}