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


#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;

struct spell {
  string name;
  long long int mp;
  string target;
  long long int damage;
};
long long int max(long long int n, long long int m) {
  if (n < m) {
    return m;
  } else {
    return n;
  }
}

long long int min(long long int n, long long int m) {
  if (n < m) {
    return n;
  } else {
    return m;
  }
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<long long int> hp(n);
    long long int max_hp = 0;
    for (int i = 0; i < n; i++) {
      cin >> hp[i];
      max_hp = max(max_hp, hp[i]);
    }
    int m;
    cin >> m;
    vector<spell> sp(m);
    for (int i = 0; i < m; i++) {
      cin >> sp[i].name >> sp[i].mp >> sp[i].target >> sp[i].damage;
    }
    long long int smp = 1000, amp = 1000, sdm = 10, adm = 10;
    for (int i = 0; i < m; i++) {
      if (sp[i].target == "Single") {
        if (smp > sp[i].mp) {
          smp = sp[i].mp;
          sdm = sp[i].damage;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      if (sp[i].target == "All") {
        if (amp > sp[i].mp) {
          amp = sp[i].mp;
          adm = sp[i].damage;
        }
      }
    }
    vector<long long int> single(max_hp + 1, INF);
    if (smp != 100) {
      single[0] = 0;
      for (int i = 1; i <= sdm && i <= max_hp; i++) {
        single[i] = smp;
        // if(i/100==0) cout << single[i]<<endl;
      }
      for (long long int i = smp + 1; i <= max_hp; i++) {
        long long int res = 114514;
        for (int k = 0; k < m; k++) {
          if (sp[k].target == "Single" &&
              single[max(0, i - sp[k].damage)] + sp[k].mp <= res)
            res = single[max(0, i - sp[k].damage)] + sp[k].mp;
        }
        if (res != 114514)
          single[i] = res;
      }
    }

    vector<long long int> all(max_hp + 1, INF);
    if (amp != 100) {
      all[0] = 0;
      for (int i = 1; i <= adm && i <= max_hp; i++) {
        all[i] = amp;
      }
      for (long long int i = adm + 1; i <= max_hp; i++) {
        long long int res = 114514;
        for (int k = 0; k < m; k++) {
          if (sp[k].target == "All" &&
              all[max(0, i - sp[k].damage)] + sp[k].mp <= res) {
            res = all[max(0, i - sp[k].damage)] + sp[k].mp;
          }
        }
        if (res != 114514)
          all[i] = res;
      }
    }
    long long int ans = 100000;
    for (int i = 0; i <= max_hp; i++) {
      vector<long long int> hpp(hp);
      int hoge = 0;
      for (int j = 0; j < n; j++) {
        hpp[j] = max(0, hpp[j] - i);
        hoge += single[hpp[j]];
      }
      ans = min(ans, all[i] + hoge);
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:



#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define INF 100000
using namespace std;

struct spell {
  string name;
  long long int mp;
  string target;
  long long int damage;
};
long long int max(long long int n, long long int m) {
  if (n < m) {
    return m;
  } else {
    return n;
  }
}

long long int min(long long int n, long long int m) {
  if (n < m) {
    return n;
  } else {
    return m;
  }
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    vector<long long int> hp(n);
    long long int max_hp = 0;
    for (int i = 0; i < n; i++) {
      cin >> hp[i];
      max_hp = max(max_hp, hp[i]);
    }
    int m;
    cin >> m;
    vector<spell> sp(m);
    for (int i = 0; i < m; i++) {
      cin >> sp[i].name >> sp[i].mp >> sp[i].target >> sp[i].damage;
    }
    long long int smp = 1000, amp = 1000, sdm = 10, adm = 10;
    for (int i = 0; i < m; i++) {
      if (sp[i].target == "Single") {
        if (smp > sp[i].mp) {
          smp = sp[i].mp;
          sdm = sp[i].damage;
        }
      }
    }
    for (int i = 0; i < m; i++) {
      if (sp[i].target == "All") {
        if (amp > sp[i].mp) {
          amp = sp[i].mp;
          adm = sp[i].damage;
        }
      }
    }
    vector<long long int> single(max_hp + 1, INF);
    if (smp != 100) {
      single[0] = 0;
      for (int i = 1; i <= sdm; i++) {
        single[i] = smp;
        // if(i/100==0) cout << single[i]<<endl;
      }
      for (long long int i = smp + 1; i <= max_hp; i++) {
        long long int res = 114514;
        for (int k = 0; k < m; k++) {
          if (sp[k].target == "Single" &&
              single[max(0, i - sp[k].damage)] + sp[k].mp <= res)
            res = single[max(0, i - sp[k].damage)] + sp[k].mp;
        }
        if (res != 114514)
          single[i] = res;
      }
    }

    vector<long long int> all(max_hp + 1, INF);
    if (amp != 100) {
      all[0] = 0;
      for (int i = 1; i <= adm; i++) {
        all[i] = amp;
      }
      for (long long int i = adm + 1; i <= max_hp; i++) {
        long long int res = 114514;
        for (int k = 0; k < m; k++) {
          if (sp[k].target == "All" &&
              all[max(0, i - sp[k].damage)] + sp[k].mp <= res) {
            res = all[max(0, i - sp[k].damage)] + sp[k].mp;
          }
        }
        if (res != 114514)
          all[i] = res;
      }
    }
    long long int ans = 100000;
    for (int i = 0; i <= max_hp; i++) {
      vector<long long int> hpp(hp);
      int hoge = 0;
      for (int j = 0; j < n; j++) {
        hpp[j] = max(0, hpp[j] - i);
        hoge += single[hpp[j]];
      }
      ans = min(ans, all[i] + hoge);
    }
    cout << ans << endl;
  }
}