Problem p03471 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#include <string>
using namespace std;

// 宣言
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

// 数値
#define MOD 1000000007
#define INF (1 << 20)
const double EPS = 1e-10;
const double PI = acos(-1.0);

// 操作
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPB(i, n) for (int i = n; i >= 0; --i)
#define SORT(A) sort(A, A + sizeof(A) / sizeof(A[0]))
#define VSORT(A) sort(A.begin(), A.end())
#define EXIST(s, e) (s.find(e) != s.end())
#define PB push_back
#define MP make_pair
#define Int(x)                                                                 \
  int x;                                                                       \
  scanf("%lld", &x);
#define int2(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%lld %lld", &x, &y);

// DEBUG
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

int main() {
  int n;
  cin >> n;
  int y;
  cin >> y;
  int a = 0, b = 0, c = 0;
  c += y % 5000 / 1000;
  a += y / 10000;
  b += (y - a * 10000) / 5000;
  if (a + b + c > n) {
    cout << "-1 -1 -1" << endl;
  } else if (a + b + c == n) {
    cout << a << " " << b << " " << c << endl;
  } else {
    int f = 1;
    while (1) {
      int e;
      e = a + b + c;
      if (f = 0 || e >= n) {
        if (e != n)
          f = 0;
        break;
      }
      if (n - e >= 9) {
        if (a) {
          a--;
          c += 10;
        } else if (b) {
          b--;
          c += 5;
        } else {
          f = 0;
        }
      } else if (n - e >= 4) {
        if (b) {
          b--;
          c += 5;
        } else if (a) {
          a--;
          b += 2;
        } else {
          f = 0;
        }
      } else {
        if (a) {
          a--;
          b += 2;
        } else {
          f = 0;
        }
      }
    }

    if (f) {
      cout << a << " " << b << " " << c << endl;
    } else {
      cout << "-1 -1 -1" << endl;
    }
  }
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
#include <string>
using namespace std;

// 宣言
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

// 数値
#define MOD 1000000007
#define INF (1 << 20)
const double EPS = 1e-10;
const double PI = acos(-1.0);

// 操作
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPB(i, n) for (int i = n; i >= 0; --i)
#define SORT(A) sort(A, A + sizeof(A) / sizeof(A[0]))
#define VSORT(A) sort(A.begin(), A.end())
#define EXIST(s, e) (s.find(e) != s.end())
#define PB push_back
#define MP make_pair
#define Int(x)                                                                 \
  int x;                                                                       \
  scanf("%lld", &x);
#define int2(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%lld %lld", &x, &y);

// DEBUG
// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;

int main() {
  int n;
  cin >> n;
  int y;
  cin >> y;
  int a = 0, b = 0, c = 0;
  c += y % 5000 / 1000;
  a += y / 10000;
  b += (y - a * 10000) / 5000;
  if (a + b + c > n) {
    cout << "-1 -1 -1" << endl;
  } else if (a + b + c == n) {
    cout << a << " " << b << " " << c << endl;
  } else {
    int f = 1;
    while (1) {
      int e;
      e = a + b + c;
      if (f == 0 || e >= n) {
        if (e != n)
          f = 0;
        break;
      }
      if (n - e >= 9) {
        if (a) {
          a--;
          c += 10;
        } else if (b) {
          b--;
          c += 5;
        } else {
          f = 0;
        }
      } else if (n - e >= 4) {
        if (b) {
          b--;
          c += 5;
        } else if (a) {
          a--;
          b += 2;
        } else {
          f = 0;
        }
      } else {
        if (a) {
          a--;
          b += 2;
        } else {
          f = 0;
        }
      }
    }

    if (f) {
      cout << a << " " << b << " " << c << endl;
    } else {
      cout << "-1 -1 -1" << endl;
    }
  }
}