alexjercan/bugnet huggingface dataset:
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

const int inf = 1 << 24;
const int pmax = 666001;
int rem[pmax];
int p, n[6], c[6] = {1, 5, 10, 50, 100, 500};

void rem_init() {
  rem[0] = 0;
  for (int i = 1; i < pmax; i++) {
    rem[i] = inf;
  }

  for (int i = 0; i <= pmax; i++) {
    for (int j = 0; j < 6; j++) {
      int next_i = i + c[j];
      if (next_i > pmax)
        continue;
      rem[next_i] = min(rem[next_i], rem[i] + 1);
    }
  }
}

void solve() {
  int pm = 0;
  for (int i = 0; i < 6; i++)
    pm += n[i] * c[i];

  int answer = 1 << 24;
  for (int i = p; i <= p + 1000; i++) {
    int k = 0, s = 0;
    for (int j = 5; j >= 0; j--) {
      if (n[j] == 0)
        continue;
      int remain = i - s;
      if (c[j] > remain)
        continue;
      int use = min(remain / c[j], n[j]);
      s += use * c[j];
      k += use;
    }
    if (s != i)
      continue;
    // cout << i << "(" << k << ")" << " no oturi ha " << i-p << "(" << rem[i-p]
    // << ")" << endl;
    answer = min(answer, k + rem[i - p]);
  }
  cout << answer << endl;
}

int main() {
  rem_init();

  while (cin >> p && p) {
    for (int i = 0; i < 6; i++) {
      cin >> n[i];
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

const int inf = 1 << 24;
const int pmax = 666001;
int rem[pmax];
int p, n[6], c[6] = {1, 5, 10, 50, 100, 500};

void rem_init() {
  rem[0] = 0;
  for (int i = 1; i < pmax; i++) {
    rem[i] = inf;
  }

  for (int i = 0; i <= pmax; i++) {
    for (int j = 0; j < 6; j++) {
      int next_i = i + c[j];
      if (next_i > pmax)
        continue;
      rem[next_i] = min(rem[next_i], rem[i] + 1);
    }
  }
}

void solve() {
  int pm = 0;
  for (int i = 0; i < 6; i++)
    pm += n[i] * c[i];

  int answer = 1 << 24;
  for (int i = p; i <= pm; i++) {
    int k = 0, s = 0;
    for (int j = 5; j >= 0; j--) {
      if (n[j] == 0)
        continue;
      int remain = i - s;
      if (c[j] > remain)
        continue;
      int use = min(remain / c[j], n[j]);
      s += use * c[j];
      k += use;
    }
    if (s != i)
      continue;
    cout << i << "(" << k << ")"
         << " no oturi ha " << i - p << "(" << rem[i - p] << ")" << endl;
    answer = min(answer, k + rem[i - p]);
  }
  cout << answer << endl;
}

int main() {
  rem_init();

  while (cin >> p && p) {
    for (int i = 0; i < 6; i++) {
      cin >> n[i];
    }
    solve();
  }
  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 28;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)

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

inline int pay(int v) {
  int cnt = 0;
  rep(i, 6) {
    int pay = min(nums[5 - i], v / N[5 - i]);
    cnt += pay;
    v -= pay * N[5 - i];
  }
  if (v == 0)
    return cnt;
  else
    return INF;
}

inline int receive(int v) {
  int cnt = 0;
  rep(i, 6) {
    int pay = v / N[5 - i];
    cnt += pay;
    v -= pay * N[5 - i];
  }
  return cnt;
}

int main() {
  int p;
  while (cin >> p && p) {
    rep(i, 6) cin >> nums[i];
    int res = INF;
    int Pmax = 0;
    rep(i, 6) Pmax += nums[i] * N[i];
    rep2(i, p, Pmax + 1) {
      int ret = i - p;
      res = min(res, pay(i) + receive(ret));
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 28;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)

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

int pay(int v) {
  int cnt = 0;
  rep(i, 6) {
    int pay = min(nums[5 - i], v / N[5 - i]);
    cnt += pay;
    v -= pay * N[5 - i];
  }
  if (v == 0)
    return cnt;
  else
    return INF;
}

int receive(int v) {
  int cnt = 0;
  rep(i, 6) {
    int pay = v / N[5 - i];
    cnt += pay;
    v -= pay * N[5 - i];
  }
  return cnt;
}

int main() {
  int p;
  while (cin >> p && p) {
    rep(i, 6) cin >> nums[i];
    int res = INF;
    int Pmax = 0;
    rep(i, 6) Pmax += nums[i] * N[i];
    rep2(i, p, Pmax + 1) {
      int ret = i - p;
      res = min(res, pay(i) + receive(ret));
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e10, EPS = 1e-9;

int coin[] = {500, 100, 50, 10, 5, 1};
int p, n[6];

int pay(int a) {
  int ret = 0;
  rep(i, 6) {
    int t = min(n[i], a / coin[i]);
    a -= coin[i] * t;
    ret += t;
  }
  return a ? inf : ret;
}
int change(int a) {
  int ret = 0;
  rep(i, 6) ret += a / coin[i], a %= coin[i];
  return ret;
}

int main() {
  while (cin >> p, p) {
    rep(i, 6) cin >> n[5 - i];
    int ans = inf;
    for (int i = p; i <= p + 1000; i++)
      ans = min(ans, pay(i) + change(i - p));
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define rp(i, c) rep(i, (c).size())
#define fr(i, c)                                                               \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = 1 << 28;
const double INF = 1e12, EPS = 1e-9;

const int MX = 500001;

int coin[] = {1, 5, 10, 50, 100, 500};
int p, n[6], lim;
int pay[MX];

int opt(
    int a) // aテδ・テつつδ」テつつョテδ」テつつ、テδ」テつづつ甘δゥテつ甘つュテδ」テつつョテδヲテつ榲つ堙δヲテつ陛つー
{
  int ret = 0;
  for (int i = 5; i >= 0; i--)
    ret += a / coin[i], a %= coin[i];

  return ret;
}
void genPay(int cur, int sum, int use) {
  rep(i, n[cur] + 1) {
    int nsum = sum + coin[cur] * i;
    if (pay[nsum] > use + i) {
      pay[nsum] = use + i;
      if (cur > 0)
        genPay(cur - 1, nsum, use + i);
    }
  }
  if (cur > 0)
    genPay(cur - 1, sum, use);
}

int main() {
  while (1) {
    cin >> p;
    rep(i, 6) cin >> n[i];
    if (p == 0)
      break;

    fill_n(pay, MX, inf);
    pay[0] = 0;
    genPay(5, 0, 0);

    lim = 0;
    int t = 5;
    while (lim < p)
      lim += min(n[t], (p - lim) / coin[t] + 1) * coin[t];

    int ans = inf;
    for (int i = p; i <= lim; i++) {
      if (pay[i] == inf)
        continue;
      ans = min(ans, opt(i - p) + pay[i]);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int memo[700000];
int c[6] = {1, 5, 10, 50, 100, 500}, cnt;
vector<int> v(6), m;

bool can(int x) {
  int res = x;
  cnt = 0;

  rep(i, 6) {
    int t = res / c[5 - i];
    t = min(t, v[5 - i]);

    res -= c[5 - i] * t;
    cnt += t;
  }

  if (res == 0)
    return true;
  else
    return false;
}

int func(int x) {
  int res = x, ret = 0;
  rep(i, 6) {
    ret += res / c[5 - i];
    res -= c[5 - i] * (res / c[5 - i]);
  }

  memo[x] = ret;
  return ret;
}

int main() {
  int p;
  rep(i, 700000) memo[i] = -1;

  while (cin >> p >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5]) {
    if (p == 0 && v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 &&
        v[4] == 0 && v[5] == 0) {
      break;
    }

    int ans = INF;
    int sum = 0;
    rep(i, 6) { sum += c[i] * v[i]; }

    REP(i, p, sum + 1) {
      if (!can(i))
        continue;
      if (memo[i - p] == -1) {
        ans = min(ans, cnt + func(i - p));
      } else {
        ans = min(ans, cnt + memo[i - p]);
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int memo[700000];
int c[6] = {1, 5, 10, 50, 100, 500}, cnt;
vector<int> v(6), m;

bool can(int x) {
  int res = x;
  cnt = 0;

  rep(i, 6) {
    int t = res / c[5 - i];
    t = min(t, v[5 - i]);

    res -= c[5 - i] * t;
    cnt += t;
  }

  if (res == 0)
    return true;
  else
    return false;
}

int func(int x) {
  int res = x, ret = 0;
  rep(i, 6) {
    ret += res / c[5 - i];
    res -= c[5 - i] * (res / c[5 - i]);
  }

  memo[x] = ret;
  return ret;
}

int main() {
  int p;
  rep(i, 700000) memo[i] = -1;

  while (cin >> p >> v[0] >> v[1] >> v[2] >> v[3] >> v[4] >> v[5]) {
    if (p == 0 && v[0] == 0 && v[1] == 0 && v[2] == 0 && v[3] == 0 &&
        v[4] == 0 && v[5] == 0) {
      break;
    }

    int ans = INF;

    REP(i, p, 700000) {
      if (!can(i))
        continue;
      if (memo[i - p] == -1) {
        ans = min(ans, cnt + func(i - p));
      } else {
        ans = min(ans, cnt + memo[i - p]);
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int P;
int A[12];
int D[] = {1, 5, 10, 50, 100, 500};
const int INF = (1 << 28);
int turi(int a) {
  if (a < 0)
    return INF;
  int ret = 0;
  for (int i = 5; i > -1; i--) {
    if (a >= D[i]) {
      ret += a / D[i];
      a = a % D[i];
    }
  }
  return ret;
}

int check(int id, int p) {
  if (p == 0)
    return 0;
  if (id < 0)
    return INF;
  if (A[id] * D[id] <= p)
    return check(id - 1, p - A[id] * D[id]) + A[id];
  else
    return check(id - 1, p % D[id]) + p / D[id];
}

int solve() {
  int sum = 0;
  for (int i = 0; i < 6; i++)
    sum += A[i] * D[i];
  int ret = INF;
  for (int i = P; i <= sum; i++) {
    int st = check(5, i);
    if (st < INF)
      ret = min(ret, st + turi(i - P));
  }
  return ret;
}

int main() {
  while (cin >> P) {
    for (int i = 0; i < 6; i++)
      cin >> A[i];
    if (!P)
      break;
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

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

int P;
int A[12];
int D[] = {1, 5, 10, 50, 100, 500};
const int INF = (1 << 28);
int turi(int a) {
  if (a < 0)
    return INF;
  int ret = 0;
  for (int i = 5; i > -1; i--) {
    if (a >= D[i]) {
      ret += a / D[i];
      a = a % D[i];
    }
  }
  return ret;
}

int dp[6][666002];
int solve(int id, int p) {
  if (dp[id][p] != -1)
    return dp[id][p];
  int ret = INF;
  int d = 0;
  if (p >= P)
    ret = turi(p - P);
  else
    d = (P - p) / D[id];
  for (int i = min(d, A[id]); i <= A[id]; i++) {
    if (id)
      ret = min(ret, solve(id - 1, p + i * D[id]) + i);
    else
      ret = min(ret, turi(p + i * D[id] - P) + i);
  }
  // cout << id << " " << p << " " << ret << endl;
  return dp[id][p] = ret;
}

int main() {
  while (cin >> P) {
    for (int i = 0; i < 6; i++)
      cin >> A[i];
    if (!P)
      break;
    memset(dp, -1, sizeof(dp));
    cout << solve(5, 0) << endl;
  }
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int coin[]{500, 100, 50, 10, 5, 1};
int n[6];

int main() {
  int p;
  while (scanf("%d", &p), p) {
    rep(i, 6) scanf("%d", &n[5 - i]);
    int Min = INT_MAX;
    for (int i = 0; i <= 500; i++) {
      int cnt = 0, k = i, d = i + p;
      rep(j, 6) {
        int t = min(k / coin[j], n[j]), y = min(d / coin[j], n[j]);
        cnt += t;
        cnt += y;
        k -= coin[j] * t;
        d -= coin[j] * y;
      }
      Min = min(Min, cnt);
    }
    printf("%d\\n", Min);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int coin[]{500, 100, 50, 10, 5, 1};
int n[6];

int main() {
  int p;
  while (scanf("%d", &p), p) {
    rep(i, 6) scanf("%d", &n[i]);
    int Min = INT_MAX;
    for (int i = 0; i <= 1000000; i++) {
      int cnt = 0, k = i, d = i + p;
      rep(j, 6) {
        cnt += k / coin[j];
        cnt += d / coin[j];
        k %= coin[j];
        d %= coin[j];
      }
      Min = min(Min, cnt);
    }
    printf("%d\\n", Min);
  }
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1 << 30;
const int C[] = {1, 5, 10, 50, 100, 500};

int main() {
  vector<int> dp(666000 + 1, 0);
  for (int i = 0; i < dp.size(); ++i) {
    int pay = i;
    for (int j = 5; j >= 0; --j) {
      int k = pay / C[j];
      pay -= k * C[j];
      dp[i] += k;
    }
  }

  int P;
  while (cin >> P) {
    if (P == 0)
      break;

    int sum = 0;
    vector<int> N(6);
    for (int i = 0; i < 6; ++i) {
      cin >> N[i];
      sum += C[i] * N[i];
    }

    int ans = INF;
    for (int i = P; i <= sum; ++i) {
      int pay = i, num = 0;
      for (int j = 5; j >= 0; --j) {
        int k = min(pay / C[j], N[j]);
        pay -= k * C[j];
        num += k;
      }

      if (pay != 0)
        continue;

      ans = min(ans, num + dp[i - P]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

const int INF = 1 << 30;
const int C[] = {1, 5, 10, 50, 100, 500};

int main() {
  int P;
  while (cin >> P) {
    if (P == 0)
      break;

    int sum = 0;
    vector<int> N(6);
    for (int i = 0; i < 6; ++i) {
      cin >> N[i];
      sum += C[i] * N[i];
    }

    int ans = INF;
    for (int i = P; i <= sum; ++i) {
      int pay = i, num = 0;
      for (int j = 5; j >= 0; --j) {
        int k = min(pay / C[j], N[j]);
        pay -= k * C[j];
        num += k;
      }

      if (pay != 0)
        continue;

      int diff = i - P;
      for (int j = 5; j >= 0; --j) {
        int k = diff / C[j];
        diff -= k * C[j];
        num += k;
      }
      ans = min(ans, num);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int p, n[6], a[] = {1, 5, 10, 50, 100, 500};
int G(int x) {
  int i, j, r = 0;
  for (i = 5; i >= 0; --i)
    for (j = 1; j <= n[i]; ++j)
      if (x >= a[i])
        x -= a[i], ++r;
      else
        break;
  return x ? 1e9 : r;
}
int F(int x) {
  int r = 0, i;
  for (i = 5; i >= 0; --i)
    r += x / a[i], x %= a[i];
  return r;
}
int main() {
  int i, x, y;
  while (scanf("%d", &p), p) {
    for (i = 0; i < 6; ++i)
      scanf("%d", n + i);
    for (x = 1e9, i = p; i < p + 501; ++i) {
      y = G(i) + F(i - p);
      if (x > y)
        x = y;
    }
    printf("%d\\n", x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int p, n[6], a[] = {1, 5, 10, 50, 100, 500}, X = 0x3fffffff;
int G(int x) {
  int i, j, r = 0;
  for (i = 5; i >= 0; --i)
    for (j = 1; j <= n[i]; ++j)
      if (x >= a[i])
        x -= a[i], ++r;
      else
        break;
  return x ? X : r;
}
int F(int x) {
  int r = 0, i;
  for (i = 5; i >= 0; --i)
    r += x / a[i], x %= a[i];
  return r;
}
int main() {
  int i, x, y, t;
  while (scanf("%d", &p), p) {
    for (t = i = 0; i < 6; ++i)
      scanf("%d", n + i), t += a[i] * n[i];
    for (x = X, i = p; i <= t; ++i) {
      y = G(i) + F(i - p);
      if (x > y)
        x = y;
    }
    printf("%d\\n", x);
  }
  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int main() {

  //  cout << 5 * 100000 + 100000 + 5 * 10000 + 10000 + 5 * 1000 + 1000 << endl;

  while (1) {
    int P, N[7] = {};
    scanf("%d", &P);
    if (P == 0)
      break;
    REP(i, 1, 7) scanf("%d", &N[i]);

    int ans = 1000000000;

    REP(d, P, P + 100000) {
      int D = d;

      int U = 0;
      int k, m;

      m = D / 500;
      k = min(m, N[6]);
      D %= 500;
      D += (m - k) * 500;
      U += k;

      m = D / 100;
      k = min(m, N[5]);
      D %= 100;
      D += (m - k) * 100;
      U += k;

      m = D / 50;
      k = min(m, N[4]);
      D %= 50;
      D += (m - k) * 50;
      U += k;

      m = D / 10;
      k = min(m, N[3]);
      D %= 10;
      D += (m - k) * 10;
      U += k;

      m = D / 5;
      k = min(m, N[2]);
      D %= 5;
      D += (m - k) * 5;
      U += k;

      m = D;
      k = min(D, N[1]);
      D += (m - k);
      U += k;

      int T = d - P;
      int S = 0;
      S += T / 500;
      T %= 500;
      S += T / 100;
      T %= 100;
      S += T / 50;
      T %= 50;
      S += T / 10;
      T %= 10;
      S += T / 5;
      T %= 5;
      S += T;

      ans = min(ans, U + S);
    }
    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)

int main() {

  //  cout << 5 * 100000 + 100000 + 5 * 10000 + 10000 + 5 * 1000 + 1000 << endl;

  while (1) {
    int P, N[7] = {};
    scanf("%d", &P);
    if (P == 0)
      break;
    REP(i, 1, 7) scanf("%d", &N[i]);

    int ans = 1000000000;

    REP(d, P, 666001) {
      int D = d;

      int U = 0;
      int k, m;

      m = D / 500;
      k = min(m, N[6]);
      D %= 500;
      D += (m - k) * 500;
      U += k;

      m = D / 100;
      k = min(m, N[5]);
      D %= 100;
      D += (m - k) * 100;
      U += k;

      m = D / 50;
      k = min(m, N[4]);
      D %= 50;
      D += (m - k) * 50;
      U += k;

      m = D / 10;
      k = min(m, N[3]);
      D %= 10;
      D += (m - k) * 10;
      U += k;

      m = D / 5;
      k = min(m, N[2]);
      D %= 5;
      D += (m - k) * 5;
      U += k;

      m = D;
      k = min(D, N[1]);
      D += (m - k);
      U += k;

      int T = d - P;
      int S = 0;
      S += T / 500;
      T %= 500;
      S += T / 100;
      T %= 100;
      S += T / 50;
      T %= 50;
      S += T / 10;
      T %= 10;
      S += T / 5;
      T %= 5;
      S += T;

      ans = min(ans, U + S);
    }
    printf("%d\\n", ans);
  }

  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF (1e9)
#define M 666001
using namespace std;
int P, d[6], c[6] = {1, 5, 10, 50, 100, 500};
int f, ans, sum, z[M];

int cal(int x) {
  int res = 0;
  for (int i = 5; i >= 0; i--)
    res += x / c[i], x = x % c[i];
  return res;
}

int cal2(int x) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    int p = min(x / c[i], d[i]);
    res += p;
    if (p)
      x -= p * c[i];
  }
  if (!x)
    return res;
  return -1;
}

int main() {
  for (int i = 0; i < M; i++)
    z[i] = cal(i);
  while (1) {
    cin >> P;
    f = sum = 0;
    for (int i = 0; i < 6; i++) {
      cin >> d[i], sum += d[i] * c[i];
      if (d[i])
        f = 1;
    }
    if (!f)
      break;
    ans = INF;
    for (int i = P; i <= sum; i++) {
      int r = cal2(i);
      if (r != -1)
        ans = min(ans, r + z[i - P]);
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF (1e9)
#define M 666001
using namespace std;
int P, d[6], c[6] = {1, 5, 10, 50, 100, 500};
int f, ans, sum;

int cal(int x) {
  int res = 0, a = x;
  for (int i = 5; i >= 0; i--)
    res += a / c[i], a = a % c[i];
  return res;
}

int cal2(int x) {
  int res = 0, a = x;
  for (int i = 5; i >= 0; i--) {
    int p = min(a / c[i], d[i]);
    res += p;
    if (p)
      a = a % (p * c[i]);
  }
  if (!a)
    return res;
  return -1;
}

int main() {
  while (1) {
    cin >> P;
    f = 0;
    for (int i = 0; i < 6; i++) {
      cin >> d[i], sum += d[i] * c[i];
      if (d[i])
        f = 1;
    }
    if (!f)
      break;
    ans = INF;
    for (int i = P; i <= sum; i++) {
      int r = cal2(i);
      if (r != -1)
        ans = min(ans, r + cal(i - P));
    }
    cout << ans << endl;
  }
  return 0;
}