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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, m, a[200], b[200], d[200];
vector<int> v1, v2, w1, w2, dp1, dp2;
vector<int> knapsack(int W, vector<int> v, vector<int> w) {
  vector<int> dp(W + 1000, 999999999);
  for (int i = 0; i < dp.size(); i++) {
    dp[i] = 999999999;
  }
  dp[0] = 0;
  for (int i = 0; i < (int)min(v.size(), w.size()); i++) {
    for (int j = v[i]; j <= W; j++)
      dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
  }
  for (int i = W - 1; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1]);
  }
  return dp;
}
int main() {
  while (true) {
    v1.clear();
    v2.clear();
    w1.clear();
    w2.clear();
    dp1.clear();
    dp2.clear();
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> m;
    for (int i = 0; i < m; i++) {
      string S, T;
      cin >> S >> b[i] >> T >> d[i];
      if (d[i] >= 200000)
        d[i] = 200000;
      if (d[i] == 0)
        continue;
      if (T == "Single") {
        v1.push_back(d[i]);
        w1.push_back(b[i]);
      }
      if (T == "All") {
        v2.push_back(d[i]);
        w2.push_back(b[i]);
      }
    }
    dp1 = knapsack(400000, v1, w1);
    dp2 = knapsack(400000, v2, w2);
    if (v1.size() == 0) {
      int maxn = 0;
      for (int i = 0; i < n; i++)
        maxn = max(maxn, a[i]);
      cout << dp2[maxn] << endl;
    } else {
      int maxn = 999999999;
      for (int i = 0; i <= 300000; i++) {
        int sum = dp2[i];
        for (int j = 0; j < n; j++) {
          if (a[j] > i && dp1[a[j] - i] <= 1e8) {
            sum += dp1[a[j] - i];
          }
        }
        maxn = min(maxn, sum);
      }
      cout << maxn << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, a[200], b[200], d[200];
vector<int> v1, v2, w1, w2, dp1, dp2;
vector<int> knapsack(int W, vector<int> v, vector<int> w) {
  vector<int> dp(W + 1000, 999999999);
  for (int i = 0; i < dp.size(); i++) {
    dp[i] = 999999999;
  }
  dp[0] = 0;
  for (int i = 0; i < (int)v.size(); i++) {
    for (int j = v[i]; j <= W; j++)
      dp[j] = min(dp[j], dp[j - v[i]] + w[i]);
  }
  for (int i = W - 1; i >= 0; i--) {
    dp[i] = min(dp[i], dp[i + 1]);
  }
  return dp;
}
int main() {
  while (true) {
    v1.clear();
    v2.clear();
    w1.clear();
    w2.clear();
    dp1.clear();
    dp2.clear();
    for (int i = 0; i < 200; i++) {
      a[i] = 0;
      b[i] = 0;
      d[i] = 0;
    }
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++)
      cin >> a[i];
    cin >> m;
    for (int i = 0; i < m; i++) {
      string S, T;
      cin >> S >> b[i] >> T >> d[i];
      if (d[i] >= 200000)
        d[i] = 200000;
      if (T == "Single") {
        v1.push_back(d[i]);
        w1.push_back(b[i]);
      }
      if (T == "All") {
        v2.push_back(d[i]);
        w2.push_back(b[i]);
      }
    }
    dp1 = knapsack(400000, v1, w1);
    dp2 = knapsack(400000, v2, w2);
    int maxn = 999999999;
    for (int i = 0; i <= 300000; i++) {
      int sum = dp2[i];
      for (int j = 0; j < n; j++) {
        int P = 0;
        if (a[j] > i)
          P = dp1[a[j] - i];
        sum += dp1[P];
      }
      maxn = min(maxn, sum);
    }
    cout << maxn << endl;
  }
  return 0;
}