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

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];

const int T = 10000;
int dp[2][T];

int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}

signed main() {
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    fill_n(*dp, 2 * T, 1001001001);
    int cnt = 0;

    for (int i = 5; i >= 0; i--) {
      while (P - C[i] >= 2500 && N[i]) {
        cnt++;
        N[i]--;
        P -= C[i];
      }
      if (N[i])
        break;
    }

    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < T; k += C[i]) {
          int w = T - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          while (deq.size() && deq.front().se + C[i] * N[i] < k)
            deq.pop_front();
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < T; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int C[6] = {1, 5, 10, 50, 100, 500};
int P;
int N[6];

int dp[2][40000];

int calc(int v) {
  int ans = 0;
  for (int i = 5; i >= 0; i--) {
    ans += v / C[i];
    v %= C[i];
  }
  return ans;
}

signed main() {
  while (cin >> P) {
    rep(i, 6) cin >> N[i];
    if (P == 0)
      break;
    memset(dp, 0, sizeof(dp));

    fill_n(*dp, 2 * 40000, 1001001001);
    int cnt = 0;
    while (P >= 40000 && N[5]) {
      P -= 500;
      N[5]--;
      cnt++;
    }
    while (P >= 40000 && N[4]) {
      P -= 100;
      N[4]--;
      cnt++;
    }

    dp[0][0] = 0;
    rep(i, 6) {
      rep(j, C[i]) {
        deque<pint> deq;
        for (int k = j; k < 50000; k += C[i]) {
          int w = 50000 - k / C[i];
          chmin(dp[(i + 1) & 1][k], dp[i & 1][k]);
          if (deq.size())
            chmin(dp[(i + 1) & 1][k], deq.front().fi - w);
          if (deq.size() && deq.front().se + N[i] * C[i] <= k)
            deq.pop_front();
          while (deq.size() && deq.back().fi >= dp[i & 1][k] + w)
            deq.pop_back();
          deq.pb(pint(dp[i & 1][k] + w, k));
        }
      }
    }
    int mi = 1001001001;
    for (int i = P; i < 50000; i++) {
      chmin(mi, dp[0][i] + calc(i - P));
    }
    cout << mi + cnt << endl;
  }
  return 0;
}