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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;

int maisuu(int sum) {
  int n = 0;

  n += sum / 500;
  sum %= 500;
  n += sum / 100;
  sum %= 100;
  n += sum / 50;
  sum %= 50;
  n += sum / 10;

  return n;
}

int main() {
  int sum;
  bool first = true;

  while (cin >> sum, sum) {
    if (first) {
      first = false;
    } else {
      cout << endl;
    }

    int ju, goju, hyaku, gohyaku;
    cin >> ju >> goju >> hyaku >> gohyaku;

    int m_mae = (ju + goju + hyaku + gohyaku);

    int shiharai;

    int m_ju = 0, m_goju = 0, m_hyaku = 0, m_gohyaku = 0;
    int m_shiharai, m_oturi;
    int m_min = 100;

    REP(a, gohyaku + 1) {
      REP(b, hyaku + 1) {
        REP(c, goju + 1) {
          REP(d, ju + 1) {
            shiharai = a * 500 + b * 100 + c * 50 + d * 10;
            m_shiharai = a + b + c + d;

            if (shiharai >= sum) {
              m_oturi = maisuu(shiharai - sum);

              if (m_min > (m_mae - m_shiharai + m_oturi)) {
                m_min = m_mae - m_shiharai + m_oturi;
                m_gohyaku = a;
                m_hyaku = b;
                m_goju = c;
                m_ju = d;
              }
            }
          }
        }
      }
    }

    if (m_ju) {
      cout << 10 << " " << m_ju << endl;
    }
    if (m_goju) {
      cout << 50 << " " << m_goju << endl;
    }
    if (m_hyaku) {
      cout << 100 << " " << m_hyaku << endl;
    }
    if (m_gohyaku) {
      cout << 500 << " " << m_gohyaku << endl;
    }
  }

  return 0;
}

Error: 9

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EACH(t, i, c) for (t::iretator i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

const double EPS = 1e-10;

int maisuu(int sum) {
  int n = 0;

  n += sum / 500;
  sum %= 500;
  n += sum / 100;
  sum %= 100;
  n += sum / 50;
  sum %= 50;
  n += sum / 10;

  return n;
}

int main() {
  int sum;

  while (cin >> sum, sum) {
    int ju, goju, hyaku, gohyaku;
    cin >> ju >> goju >> hyaku >> gohyaku;

    int m_mae = (ju + goju + hyaku + gohyaku);

    int shiharai;

    int m_ju, m_goju, m_hyaku, m_gohyaku;
    int m_shiharai, m_oturi;
    int m_min = 100;

    REP(a, gohyaku + 1) {
      REP(b, hyaku + 1) {
        REP(c, goju + 1) {
          REP(d, ju + 1) {
            shiharai = a * 500 + b * 100 + c * 50 + d * 10;
            m_shiharai = a + b + c + d;

            if (shiharai >= sum) {
              m_oturi = maisuu(shiharai - sum);

              if (m_min > (m_mae - m_shiharai + m_oturi)) {
                m_min = m_mae - m_shiharai + m_oturi;
                m_gohyaku = a;
                m_hyaku = b;
                m_goju = c;
                m_ju = d;
              }
            }
          }
        }
      }
    }

    if (m_ju) {
      cout << "10 " << m_ju << endl;
    }
    if (m_goju) {
      cout << "50 " << m_goju << endl;
    }
    if (m_hyaku) {
      cout << "100 " << m_hyaku << endl;
    }
    if (m_gohyaku) {
      cout << "500 " << m_gohyaku << endl;
    }

    cout << endl;
  }

  return 9;
}