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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#

#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

typedef complex<int> POINT;
template <class T> class vevector : public vector<vector<T>> {
public:
  using vector<vector<T>>::operator[];
  T &operator[](const POINT &index) {
    return (*this)[index.real()][index.imag()];
  }
};

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <> POINT read<POINT>() {
  int x, y;
  cin >> x >> y;
  return POINT(x, y);
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) {
  cout << t << ' ' << t2 << endl;
}

template <class T> void write(const vector<T> &v) {
  ostringstream ss;
  for (auto x : v)
    ss << x << ' ';
  auto s = ss.str();
  cout << s.substr(0, s.length() - 1) << endl;
}

template <class T> string toString(const T &t) {
  stringstream ss;
  ss << t;
  return ss.str();
}

struct _Reader {
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
#define READ(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  _Reader(), __VA_ARGS__
#define countof(a) (sizeof(a) / sizeof(a[0]))

void solve();
int main() {
  solve();
  return 0;
}

vector<unsigned char> isPrime;
vector<int> primes;
void initPrimes(int n) {
  isPrime = vector<unsigned char>(n + 1, true);
  isPrime[0] = isPrime[1] = false;
  FOR(i, 2, n) {
    if (!isPrime[i])
      continue;
    primes.push_back(i);
    for (int j = i * 2; j <= n; j += i)
      isPrime[j] = false;
  }
}

/// template end ///

void solve() {
  const int _coinTypes[] = {10, 50, 100, 500};
  const int C = countof(_coinTypes);
  const auto coinTypes = vector<int>(_coinTypes, _coinTypes + C);
  REP(testcase, INF) {
    READ(int, price);
    if (!price) {
      break;
    }
    if (testcase != 0) {
      cout << endl;
    }

    vector<int> wallet = read<int>(coinTypes.size());
    auto account = inner_product(ALLOF(wallet), coinTypes.begin(), 0);

    vector<int> charge(C);
    int totalCharge = account - price;
    RREP(i, C) {
      charge[i] = totalCharge / coinTypes[i];
      totalCharge %= coinTypes[i];
    }

    vector<int> payment(C);
    REP(i, C) { payment[i] = max(wallet[i] - charge[i], 0); }
    REP(i, C) {
      if (payment[i]) {
        write(coinTypes[i], payment[i]);
      }
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#

#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s); i < (n); i++)
#define ALLOF(c) c.begin(), c.end()
#define RANGEOF(c, i, n) c.begin() + (i), c.begin() + (i) + (n)
#define EPS 1e-9
#define INF 1000000000
using namespace std;

typedef complex<int> POINT;
template <class T> class vevector : public vector<vector<T>> {
public:
  using vector<vector<T>>::operator[];
  T &operator[](const POINT &index) {
    return (*this)[index.real()][index.imag()];
  }
};

template <class T> T read() {
  T t;
  cin >> t;
  return t;
}
template <> POINT read<POINT>() {
  int x, y;
  cin >> x >> y;
  return POINT(x, y);
}
template <class T> vector<T> read(int n) {
  vector<T> v;
  REP(i, n) { v.push_back(read<T>()); }
  return v;
}
template <class T> vevector<T> read(int n, int m) {
  vevector<T> v;
  REP(i, n) v.push_back(read<T>(m));
  return v;
}
template <class T> void write(const T &t) { cout << t << endl; }
template <class T> void write(const T &t, const T &t2) {
  cout << t << ' ' << t2 << endl;
}

template <class T> void write(const vector<T> &v) {
  ostringstream ss;
  for (auto x : v)
    ss << x << ' ';
  auto s = ss.str();
  cout << s.substr(0, s.length() - 1) << endl;
}

template <class T> string toString(const T &t) {
  stringstream ss;
  ss << t;
  return ss.str();
}

struct _Reader {
  template <class T> _Reader operator,(T &rhs) {
    cin >> rhs;
    return *this;
  }
};
#define READ(t, ...)                                                           \\
  t __VA_ARGS__;                                                               \\
  _Reader(), __VA_ARGS__
#define countof(a) (sizeof(a) / sizeof(a[0]))

void solve();
int main() {
  solve();
  return 0;
}

vector<unsigned char> isPrime;
vector<int> primes;
void initPrimes(int n) {
  isPrime = vector<unsigned char>(n + 1, true);
  isPrime[0] = isPrime[1] = false;
  FOR(i, 2, n) {
    if (!isPrime[i])
      continue;
    primes.push_back(i);
    for (int j = i * 2; j <= n; j += i)
      isPrime[j] = false;
  }
}

/// template end ///

void solve() {
  const int _coinTypes[] = {10, 50, 100, 500};
  const int C = countof(_coinTypes);
  const auto coinTypes = vector<int>(_coinTypes, _coinTypes + C);
  REP(testcase, INF) {
    READ(int, price);
    if (!price) {
      break;
    }
    if (testcase != 0) {
      cout << endl;
    }

    vector<int> money = read<int>(coinTypes.size());
    auto value = inner_product(ALLOF(money), coinTypes.begin(), 0);

    vector<int> charge;
    int cvalue = value - price;
    RREP(i, C) {
      charge.push_back(cvalue / coinTypes[i]);
      cvalue %= coinTypes[i];
    }
    reverse(ALLOF(charge));

    vector<int> payment;
    REP(i, C) { charge.push_back(max(money[i] - charge[i], 0)); }
    REP(i, C) {
      if (payment[i]) {
        write(coinTypes[i], payment[i]);
      }
    }
  }
}