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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int dp[(1 << 15) + 5], n, m, s[20], d[120];
int main() {
  while (1) {
    cin >> n >> m;
    if (!n)
      return 0;
    int all = 0, ret = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      all += s[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i];
      if (d[i] > all)
        ret += d[i] - all;
    }
    sort(d, d + m);
    for (int i = 0; i < (1 << n); i++)
      dp[i] = INF;
    dp[0] = 0;
    for (int i = 0; i < (1 << n); i++) {
      int sum = 0;
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1))
          sum += s[j];
      }
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1))
          continue;
        int x = upper_bound(d, d + m, sum) - d;
        int y = upper_bound(d, d + m, sum + s[j]) - d;
        int z = dp[i];
        for (int k = x; k < y; k++) {
          z += min(d[k] - sum, sum + s[j] - d[k]);
        }
        dp[i + (1 << j)] = min(dp[i + (1 << j)], z);
      }
    }
    cout << dp[(1 << n) - 1] + ret << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> P1;
typedef pair<P, P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i, x) for (int i = 0; i < x; i++)
#define SORT(x) sort(x.begin(), x.end())
#define ERASE(x) x.erase(unique(x.begin(), x.end()), x.end())
#define POSL(x, v) (lower_bound(x.begin(), x.end(), v) - x.begin())
#define POSU(x, v) (upper_bound(x.begin(), x.end(), v) - x.begin())
int dp[(1 << 15) + 5], n, m, s[20], d[20];
int main() {
  while (1) {
    cin >> n >> m;
    if (!n)
      return 0;
    int all = 0, ret = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      all += s[i];
    }
    for (int i = 0; i < m; i++) {
      cin >> d[i];
      if (d[i] > all)
        ret += d[i] - all;
    }
    sort(d, d + m);
    for (int i = 0; i < (1 << n); i++)
      dp[i] = INF;
    dp[0] = 0;
    for (int i = 0; i < (1 << n); i++) {
      int sum = 0;
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1))
          sum += s[j];
      }
      for (int j = 0; j < n; j++) {
        if (((i >> j) & 1))
          continue;
        int x = upper_bound(d, d + m, sum) - d;
        int y = upper_bound(d, d + m, sum + s[j]) - d;
        int z = dp[i];
        for (int k = x; k < y; k++) {
          z += min(d[k] - sum, sum + s[j] - d[k]);
        }
        dp[i + (1 << j)] = min(dp[i + (1 << j)], z);
      }
    }
    cout << dp[(1 << n) - 1] + ret << endl;
  }
}