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

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

__int128 N;
__int128 f(__int128 x) { return (N + 2 * x - 1) / (2 * x); }

__int128 s1(long long N) {
  __int128 sum = 0;
  long long T = sqrt(N / 2) - 1e-10;
  T++;
  __int128 now = N / 2;
  for (long long i = 1; i <= T; i++) {
    sum += N / 2 - now;
    sum += N / 2 - now;
    now = f(i);
  }
  sum += (__int128)(N / 2 - T) * (N / 2 - T);

  return sum;
}

int main() {
  long long N_;
  while (cin >> N_ && N_) {
    N = N_;
    __int128 sum = s1(N);
    __int128 result = 4 * (2 * N) + 2 * N * N - 8 * sum;
    string s;
    while (result > 0) {
      s = string(1, '0' + (int)(result % 10)) + s;
      result /= 10;
    }
    cout << s << endl;
  }
}

Error: TLE

Fixed submission:

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

__int128 N;
__int128 f(__int128 x) { return (N + 2 * x - 1) / (2 * x); }

__int128 s1(__int128 N) {
  __int128 sum = 0;
  __int128 now = N / 2;
  for (int i = 1; i <= N / 2; i++) {
    sum += N / 2 - now;
    now = f(i);
  }
  return sum;
}

__int128 s2(long long N) {
  __int128 sum = 0;
  long long i = 1;
  while (i != N / 2) {
    long long v = f(i);
    long long l = i + 1, r = N / 2;
    while (l != r) {
      __int128 m = (l + r) / 2;
      if (v != f(m)) {
        r = m;
      } else {
        l = m + 1;
      }
    }
    sum += (__int128)(N / 2 - v) * (l - i);
    i = r;
  }
  return sum;
}

int main() {
  long long N_;
  while (cin >> N_ && N_) {
    N = N_;
    __int128 sum2 = s2(N);
    __int128 result = 4 * (2 * N) + 2 * N * N - sum2;
    string s;
    while (result > 0) {
      s = string(1, '0' + (int)(result % 10)) + s;
      result /= 10;
    }
    cout << s << endl;
  }
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int main() {
  int K, C, count;
  int i;
  K = 1;
  C = 0;
  count = 0;
  scanf("%d", &K);
  while (K != 0) {
    C = 0;
    count = 0;
    for (i = 0; i < K * (K - 1) / 2; i++) {
      scanf("%d", &C);
      count = count + C;
    }
    count = count / (K - 1);
    printf("%d\\n", count);
    scanf("%d", &K);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>

int main() {
  int K, C, count;
  int i;

  do {
    scanf("%d", K);
    for (i = 0; i < K * (K - 1) / 2; i++) {
      scanf("%d", C);
      count = +C;
    }
    printf("%d", count);
  } while (K != 0);
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int k, l;
  int p = 0;
  int i = 0;
  int a[10000];
  while (1) {
    cin >> k;
    l = k * (k - 1) / 2;
    if (k == 0) {
      break;
    }
    for (i = 0; i < l; i++) {
      cin >> a[i];
      p += a[i];
    }
    cout << p / (k - 1) << endl;
    p = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int k, l;
  int p = 0;
  int i = 0;
  int a[101];
  while (1) {
    cin >> k;
    l = k * (k - 1) / 2;
    if (k == 0) {
      break;
    }
    for (i = 0; i < l; i++) {
      cin >> a[i];
      p += a[i];
    }
    cout << p / (k - 1) << endl;
    p = 0;
  }
  return 0;
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  while (1) {
    int K, c[10000], sum = 0;
    cin >> K;
    if (0 == K)
      break;
    for (int i = 0; i < K * (K - 1) / 2; i++) {
      cin >> c[i];
      sum += c[i];
    }
    sum = sum / (K - 1);
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  while (1) {
    int K, c[10], sum = 0;
    cin >> K;
    if (0 == K)
      break;
    for (int i = 0; i < K * (K - 1) / 2; i++) {
      cin >> c[i];
      sum += c[i];
    }
    sum = sum / (K - 1);
    cout << sum << endl;
  }
  return 0;
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  for (;;) {
    int k, al, a[10000];
    al = 0;
    cin >> k;
    if (k == 0) {
      break;
    }
    for (int i = 0; 2 * i < k * (k - 1); i++) {
      cin >> a[i];
      al += a[i];
    }
    cout << al / (k - 1) << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  while (1) {
    int k, al, a[1000];
    al = 0;
    cin >> k;
    if (k == 0) {
      break;
    }
    for (int i = 0; i < k * (k - 1) / 2; i++) {
      cin >> a[i];
      al += a[i];
    }
    cout << al / (k - 1) << endl;
  }

  return 0;
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int k, c, sum, cou = 0, ans[100];
  while (cin >> k && k != 0) {
    sum = 0;
    for (int i = 0; i < k * (k - 1) / 2; i++) {
      cin >> c;
      sum += c;
    }
    ans[cou] = sum / (k - 1);
    cou++;
  }
  for (int i = 0; i < cou; i++) {
    cout << ans[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int k, c[100] = {0}, sum, cou = 0, ans[100];
  while (cin >> k) {
    if (k == 0)
      break;
    sum = 0;
    for (int i = 0; i < k * (k - 1) / 2; i++) {
      cin >> c[i];
      sum += c[i];
    }
    ans[cou] = sum / (k - 1);
    cou++;
  }
  for (int i = 0; i < cou; i++) {
    cout << ans[i] << endl;
  }
}
Problem p00613 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {

  while (1) {

    int k;
    int kazu, sum = 0;

    cin >> k;

    if (k == 0) {
      break;
    }

    for (int i = 0; i < k * (k - 1) / 2; i++) {
      cin >> kazu;

      sum += kazu;
    }

    cout << sum / (k - 1) << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  while (1) {
    int k, kazu;
    int sum = 0;

    cin >> k;

    if (k == 0) {
      break;
    }

    for (int i = 0; i < k * (k - 1) / 2; k++) {
      cin >> kazu;

      sum += kazu;
    }

    cout << sum / (k - 1) << endl;
  }
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << x << endl;
using namespace std;

int main() {
  int k, sum, di;
  int c;

  while (cin >> k, k) {
    sum = 0;
    rep(i, k * (k - 1) / 2) {
      cin >> c;
      sum += c;
    }
    cout << sum / (k - 1) << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << x << endl;
using namespace std;

int main() {
  int k, sum, di;
  int c;

  while (cin >> k, k) {
    sum = 0;
    di = 1;
    range(i, 1, k + 1) {
      if (i < k + (k - 1) / 2) {
        cin >> c;
        sum += c;
      }
      di *= i;
    }
    cout << sum / (di / k) << endl;
  }
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int a;
  while (cin >> a, a) {
    int b = a * (a - 1) / 2;
    b = a * (a - 1) / 2;
    int x = 0;
    for (int c = 0; c < b; c++) {
      int d;
      cin >> d;
      x += d;
    }
    cout << x / (a - 1) << endl;
  }
}

Error: -8

Fixed submission:

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

int parse(string a) {
  int s = 0;
  for (char b : a) {
    s *= 10;
    s += b - 48;
  }
  return s;
}
vector<int> split(string a) {
  vector<int> b;
  string c = "";
  for (char d : a) {
    if (d == ' ') {
      b.push_back(parse(c));
      c = "";
    } else
      c += d;
  }
  b.push_back(parse(c));
  return b;
}
int main() {
  int a;
  while (cin >> a, a) {
    string e;
    getline(cin, e);
    vector<int> b = split(e);
    int c = 0;
    for (int d : b) {
      c += d;
    }
    cout << c / (b.size() - 1) << endl;
  }
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-8;
#define pb emplace_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef pair<string, int> Psi;
typedef pair<ll, ll> Pll;

int main() {
  int K;
  while (cin >> K, K) {
    int C = K * (K - 1) / 2;
    int sum = 0;
    rep(i, C) {
      int tmp;
      cin >> tmp;
      sum += tmp;
    }
    cout << sum / (K - 1) << endl;
  }

  return 0;
}

Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1e9;
const long long LINF = 1e18;
const double EPS = 1e-8;
#define pb emplace_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); ++i)
#define rep(i, j) reps(i, 0, j)
#define all(a) (a).begin(), (a).end()
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef pair<string, int> Psi;
typedef pair<ll, ll> Pll;

int main() {
  int K;
  while ((cin >> K), K) {
    int C = K * (K - 1) / 2;
    int sum = 0;
    rep(i, C) {
      int tmp;
      cin >> tmp;
      sum += tmp;
    }
    int ans = sum / (C - 1);
    printf("%d\\n", ans);
  }

  return 0;
}
Problem p00613 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int k;
  while (cin >> k && k) {
    int res = 0;
    for (int i = 0; i < k * (k - 1) / 2; ++i) {
      int d;
      cin >> d;
      res += d;
    }
    res /= k - 1;
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int k;
  while (cin >> k && k) {
    int D[k];
    for (int i = 0; i < k * (k - 1) / 2; ++i) {
      cin >> D[i];
    }
    int sum = 0;
    for (int i = 0; i < k * (k - 1) / 2; ++i) {
      sum += D[i];
    }
    cout << sum / (k - 1) << endl;
  }
  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int min(int x, int y) { return x < y ? x : y; }

int main() {
  const int inf = 1000000000;
  REP(p, 1, 666667) if (!ret[p]) {
    int t = p;
    rep(i, 6) {
      int k = t / a[5 - i];
      ret[p] += k;
      t -= k * a[5 - i];
    }
  }
  int P;
  while (scanf("%d", &P), P) {
    int s = 0;
    rep(i, 6) {
      scanf("%d", N + i);
      s += a[i] * N[i];
    }

    int res = inf;
    REP(p, P, s + 1) {
      int t = p;
      int pay = 0;
      rep(i, 6) {
        int k = min(N[5 - i], t / a[5 - i]);
        pay += k;
        t -= k * a[5 - i];
      }
      if (!t)
        res = min(res, pay + ret[p - P]);
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int a[] = {1, 5, 10, 50, 100, 500};
int N[6];
int pay[666667], ret[666667];

int min(int x, int y) { return x < y ? x : y; }

int main() {
  const int inf = 1000000000;
  REP(p, 1, 666667) if (!ret[p]) {
    int t = p;
    rep(i, 6) {
      int k = t / a[5 - i];
      ret[p] += k;
      t -= k * a[5 - i];
    }
  }
  int P;
  while (scanf("%d", &P), P) {
    memset(pay, 0, sizeof(pay));
    int s = 0;
    rep(i, 6) {
      scanf("%d", N + i);
      s += a[i] * N[i];
    }

    REP(p, 1, s + 1) if (!pay[p]) {
      int t = p;
      int c = 0;
      rep(i, 6) {
        int k = min(N[5 - i], t / a[5 - i]);
        c += k;
        t -= k * a[5 - i];
      }
      if (!t)
        pay[p] = c;
    }
    int res = inf;
    REP(p, P, s + 1) if (pay[p]) res = min(res, pay[p] + ret[p - P]);
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using namespace std;
int item[] = {1, 5, 10, 50, 100, 500};
int num[6];

int cash(int a) {
  int t_num[6];
  int ret = 0;
  for (int i = 0; i < 6; i++) {
    t_num[i] = num[i];
  }
  for (int i = 5; i >= 0; i--) {
    if (a >= item[i] && t_num[i]) {
      a -= item[i];
      t_num[i]--;
      ret++;
      i++;
    }
  }
  if (a != 0)
    return -1;
  return ret;
}

int plz(int a) {
  int ret = 0;
  for (int i = 5; i >= 0; i--) {
    if (a >= item[i]) {
      a -= item[i];
      i++;
      ret++;
    }
  }
  return ret;
}

int main() {
  int m, n, t, ret;
  while (cin >> m && m) {
    int ret = (1 << 21);
    for (int i = 0; i < 6; i++) {
      cin >> num[i];
    }
    for (int i = 0; i < 500; i++) {
      if (~cash(m + i)) {
        ret = min(ret, cash(m + i) + plz(i));
      }
    }
    cout << ret << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int item[] = {1, 5, 10, 50, 100, 500};
int num[6];

int cash(int a) {
  int t_num[6];
  int ret = 0;
  for (int i = 0; i < 6; i++) {
    t_num[i] = num[i];
  }
  for (int i = 5; i >= 0; i--) {
    if (a >= item[i] && t_num[i]) {
      a -= item[i];
      t_num[i]--;
      ret++;
      i++;
    }
  }
  return ret;
}

int plz(int a) {
  int ret = 0;
  for (int i = 5; i >= 0; i--) {
    if (a >= item[i]) {
      a -= item[i];
      i++;
      ret++;
    }
  }
  return ret;
}

int main() {
  int m, n, t;
  while (cin >> m && m) {
    vector<bool> data(10001, false), tmp;
    data[0] = true;
    for (int i = 0; i < 6; i++) {
      cin >> num[i];
    }
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < num[i]; j++) {
        tmp = data;
        for (int k = 0; k + item[i] < 10001; k++) {
          data[k + item[i]] = tmp[k + item[i]] | tmp[k];
        }
      }
    }
    int ret = (1 << 21);
    for (int k = m; k <= 10001; k++) {
      if (data[k]) {
        ret = min(ret, cash(k) + plz(k - m));
      }
    }
    cout << ret << endl;
  }
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

// ツ催渉ャツづ個津楪銭ツづ個鳴青板づーツ陛板つキ
int change(int x) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    res += x / m[i];
    x -= m[i] * (x / m[i]);
  }
  return res;
}

// ツ篠ゥツ閉ェツつェツ篠敖づづつ「ツづゥツ硬ツ嘉敖づ可ソツ格ツづーツつソツづ・ツつ、ツづ支ツ閉・ツつヲツづゥツつゥツづ個板サツ津ィ
int pay(int x) {
  int res = 0;

  for (int i = 5; i >= 0; i--) {
    int tmp = x / m[i];

    if (tmp <= n[i]) {
      res += tmp;
      x -= m[i] * tmp;
    } else {
      res += n[i];
      x -= m[i] * n[i];
    }
  }

  return x == 0 ? res : -1;
}

int main(void) {
  while (scanf("%d", &p), p) {
    int sum = 0;
    for (int i = 0; i < 6; i++) {
      scanf("%d", &n[i]);
      sum += n[i] * m[i];
    }

    int ans = 999999999;
    for (int i = p; i <= sum; i++) {
      int tmp = pay(i);
      if (tmp != -1) {
        if (i != p) {
          if (changeMemo[i - p] == 0)
            changeMemo[i - p] = change(i - p);
          tmp += changeMemo[i - p];
        }
        if (ans > tmp)
          ans = tmp;
      }
    }

    printf("%d\\n", ans);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

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

// ツ催渉ャツづ個津楪銭ツづ個鳴青板づーツ陛板つキ
int change(int x) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    res += x / m[i];
    x -= m[i] * (int)(x / m[i]);
  }
  return res;
}

// ツ篠ゥツ閉ェツつェツ篠敖づづつ「ツづゥツ硬ツ嘉敖づ可ソツ格ツづーツつソツづ・ツつ、ツづ支ツ閉・ツつヲツづゥツつゥツづ個板サツ津ィ
int pay(int x) {
  int res = 0;

  for (int i = 5; i >= 0; i--) {
    if (x >= m[i]) {
      int tmp = x / m[i];
      if (tmp <= n[i]) {
        res += tmp;
        x -= m[i] * tmp;
      } else {
        res += n[i];
        x -= m[i] * n[i];
      }
    }
  }

  return x == 0 ? res : -1;
}

int main(void) {
  while (scanf("%d", &p), p) {
    int sum = 0;
    for (int i = 0; i < 6; i++) {
      scanf("%d", &n[i]);
      sum += n[i] * m[i];
    }

    int ans = 999999999;
    for (int i = p; i <= sum; i++) {
      int tmp = pay(i);
      if (tmp != -1) {
        if (i != p)
          tmp += (changeMemo[i - p] == 0 ? change(i - p) : changeMemo[i - p]);
        if (ans > tmp)
          ans = tmp;
      }
    }

    printf("%d\\n", ans);
  }

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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FOREACH(i, c)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

const int INF = 1000000000;
int value[] = {500, 100, 50, 10, 5, 1};

int opt(int p, int num[]) {
  int ret = 0;
  REP(i, 6) {
    int use = min(num[i], p / value[i]);
    p -= value[i] * use;
    ret += use;
  }
  return p ? INF : ret;
}

int main() {
  int p;
  while (scanf("%d", &p), p) {
    int num[6], sum = 0;
    REP(i, 6) {
      scanf("%d", &num[5 - i]);
      sum += num[5 - i] * value[5 - i];
    }
    int big[] = {INF, INF, INF, INF, INF, INF};
    int ans = INF;
    FOREQ(i, p, min(sum, p + 10000)) {
      ans = min(ans, opt(i, num) + opt(i - p, big));
    }
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define FOREQ(i, a, b) for (int i = (a); i <= (int)(b); ++i)
#define FOREACH(i, c)                                                          \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

const int INF = 1000000000;
int value[] = {500, 100, 50, 10, 5, 1};

int opt(int p, int num[]) {
  int ret = 0;
  REP(i, 6) {
    int use = min(num[i], p / value[i]);
    p -= value[i] * use;
    ret += use;
  }
  return p ? INF : ret;
}

int main() {
  int p;
  while (scanf("%d", &p), p) {
    int num[6], sum = 0;
    REP(i, 6) {
      scanf("%d", &num[5 - i]);
      sum += num[5 - i] * value[5 - i];
    }
    int big[] = {INF, INF, INF, INF, INF, INF};
    int ans = INF;
    FOREQ(i, p, sum) { ans = min(ans, opt(i, num) + opt(i - p, big)); }
    printf("%d\\n", ans);
  }
}
Problem p00614 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int MAXN = 666 * 1000 + 1;

int dp2[MAXN];
int p[6] = {1, 5, 10, 50, 100, 500};
int count_c(int Y, int c[]) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    int t = min(c[i], Y / p[i]);
    Y -= t * p[i];
    res += t;
  }
  if (Y == 0)
    return res;
  else
    return INF;
}
int main() {
  int Y;
  fill(dp2, dp2 + MAXN, INF);
  dp2[0] = 0;
  REP(i, 6) {
    FOR(j, p[i], MAXN) { dp2[j] = min(dp2[j], dp2[j - p[i]] + 1); }
  }
  while (cin >> Y, Y) {
    int c[6];
    REP(i, 6) cin >> c[i];
    int MY = 0;
    REP(i, 6) MY += c[i] * p[i];
    int ans = INF;
    for (int i = MY; i >= Y; i--) {
      ans = min(ans, count_c(i, c) + dp2[i - Y]);
    }
    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 <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
const int MAXN = 666 * 1000 + 1;

int dp2[MAXN];
int p[6] = {1, 5, 10, 50, 100, 500};
int count_c(int Y, int c[]) {
  int res = 0;
  for (int i = 5; i >= 0; i--) {
    REP(j, c[i]) {
      if (Y >= p[i]) {
        Y -= p[i];
        res++;
      }
    }
  }
  if (Y == 0)
    return res;
  else
    return INF;
}
int main() {
  int Y;
  fill(dp2, dp2 + MAXN, INF);
  dp2[0] = 0;
  REP(i, 6) {
    FOR(j, p[i], MAXN) { dp2[j] = min(dp2[j], dp2[j - p[i]] + 1); }
  }
  while (cin >> Y, Y) {
    int c[6];
    REP(i, 6) cin >> c[i];
    int MY = 0;
    REP(i, 6) MY += c[i] * p[i];
    int ans = INF;
    for (int i = MY; i >= Y; i--) {
      ans = min(ans, count_c(i, c) + dp2[i - Y]);
    }
    cout << ans << endl;
  }
  return 0;
}