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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

const int INF = numeric_limits<int>::max();

void fillDP(const vector<pair<int, long int>> &skills, vector<int> &dp,
            long int size) {
  dp.resize(size + 1);
  fill(dp.begin(), dp.end(), INF);
  dp[0] = 0;
  for (auto itr = skills.begin(); itr != skills.end(); itr++) {
    for (long int i = 0; i <= size; i++) {
      if (dp[i] == INF) {
        continue;
      }
      long int damage = min(i + itr->second, size);
      dp[damage] = min(dp[damage], dp[i] + itr->first);
    }
  }
}

int main() {
  while (1) {
    int enemyNumber, skillNumber;
    long int maxHP;
    vector<int> allDP, singleDP;
    vector<long int> hps;
    vector<pair<int, long int>> allSkills, singleSkills;

    cin >> enemyNumber;
    if (enemyNumber == 0) {
      break;
    }
    for (int i = 0; i < enemyNumber; i++) {
      long int hp;
      cin >> hp;
      hps.push_back(hp);
    }

    cin >> skillNumber;
    for (int i = 0; i < skillNumber; i++) {
      int mp;
      long int damage;
      string name, target;
      cin >> name >> mp >> target >> damage;
      if (target == "All") {
        allSkills.push_back(make_pair(mp, damage));
      } else {
        singleSkills.push_back(make_pair(mp, damage));
      }
    }

    maxHP = *max_element(hps.begin(), hps.end());
    fillDP(allSkills, allDP, maxHP);
    fillDP(singleSkills, singleDP, maxHP);

    int minimum = INF;
    for (long int i = maxHP; i > 0; i--) {
      minimum = min(minimum, singleDP[i]);
      singleDP[i] = minimum;
    }

    long int answer = (long int)INF;

    for (long int i = 0; i <= maxHP; i++) {
      if (allDP[i] == INF) {
        continue;
      }

      long int cost = allDP[i];

      for (int j = 0; j < enemyNumber; j++) {
        if (hps[j] - i > 0) {
          cost += singleDP[hps[j] - i];
        }
      }

      answer = min(answer, cost);
    }

    cout << answer << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

class Magic {
public:
  int mp;
  int damage;
  bool isAll;
};

int calcMP(std::vector<Magic> magics, std::vector<int> enemyHPs) {
  int totalMP = std::numeric_limits<int>::max();
  if (magics.empty()) {
    return totalMP;
  }

  bool defeated = true;
  for (auto itr = enemyHPs.begin(); itr != enemyHPs.end(); itr++) {
    if (*itr > 0) {
      defeated = false;
      break;
    }
  }
  if (defeated) {
    return 0;
  }

  if (!magics.back().isAll) {
    for (int i = 0; i < (int)enemyHPs.size(); i++) {
      if (enemyHPs[i] <= 0) {
        continue;
      }
      std::vector<int> tempEnemyHPs = enemyHPs;
      tempEnemyHPs[i] -= magics.back().damage;
      totalMP =
          std::min(totalMP, calcMP(magics, tempEnemyHPs) + magics.back().mp);
    }
  } else {
    std::vector<int> tempEnemyHPs = enemyHPs;
    for (auto itr = tempEnemyHPs.begin(); itr != tempEnemyHPs.end(); itr++) {
      *itr -= magics.back().damage;
    }
    totalMP =
        std::min(totalMP, calcMP(magics, tempEnemyHPs) + magics.back().mp);
  }

  magics.pop_back();
  totalMP = std::min(totalMP, calcMP(magics, enemyHPs));

  return totalMP;
}

int main() {
  while (1) {
    std::vector<int> enemyVector;
    std::vector<Magic> magicVector;
    int enemyNumber, magicNumber;

    std::cin >> enemyNumber;
    if (enemyNumber == 0) {
      break;
    }
    for (int i = 0; i < enemyNumber; i++) {
      int hp;
      std::cin >> hp;
      enemyVector.push_back(hp);
    }

    std::cin >> magicNumber;
    for (int i = 0; i < magicNumber; i++) {
      std::string name, target;
      Magic magic;
      std::cin >> name >> magic.mp >> target >> magic.damage;
      if (target == "All") {
        magic.isAll = true;
      } else {
        magic.isAll = false;
      }
      magicVector.push_back(magic);
    }

    std::cout << calcMP(magicVector, enemyVector) << std::endl;
  }
}