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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int main() {
  bool f = true;
  while (1) {
    int m;
    cin >> m;
    if (m == 0) {
      break;
    }
    if (!f)
      cout << endl;
    f = false;
    int t[4] = {10, 50, 100, 500};
    int c[4] = {}, k[4] = {};
    rep(i, 4) { // 10,50,100,500
      cin >> c[i];
    }
    int sum = 0;
    rep(i, 4) { sum += c[i] * t[i]; }
    sum -= m;
    for (int i = 3; i >= 0; i--) {
      k[i] = sum / t[i];
      sum %= t[i];
    }

    rep(i, 4) {
      if (c[i] - k[i] > 0) {
        cout << t[i] << " " << c[i] - k[i] << endl;
      }
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template <class T> using vv = vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;

int main() {
  while (1) {
    int m;
    cin >> m;
    if (m == 0) {
      break;
    }
    int t[4] = {10, 50, 100, 500};
    int c[4] = {}, k[4] = {};
    rep(i, 4) { // 10,50,100,500
      cin >> c[i];
    }

    rep(i, 3) {
      if (c[i] * t[i] >= m) {
        if (m % t[i] == 0) {
          k[i] = m / t[i];
          m = 0;
        } else {
          k[i] = m / t[i] + 1;
          m = 0;
        }
      } else if (t[i] * c[i] >= m % t[i + 1]) {
        k[i] += (m % t[i + 1]) / t[i];
        c[i] -= k[i];
        m -= m % t[i + 1];
        if (c[i] * t[i] >= t[i + 1]) {
          int tmp = (c[i] / (t[i + 1] / t[i])) * (t[i + 1] / t[i]);
          k[i] += tmp;
          c[i] -= tmp;
          m -= tmp * t[i];
        }
      }
      debug(m);
    }
    if (m % t[3] == 0) {
      k[3] = m / t[3];
    } else {
      k[3] = m / t[3] + 1;
    }

    rep(i, 4) {
      if (k[i] != 0) {
        cout << t[i] << " " << k[i] << endl;
      }
    }
  }
  return 0;
}