Language: C++

#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>

using namespace std;

enum Coin {
  N1 = 0, N5 = 1, N10 = 2, N50 = 3, N100 = 4, N500 = 5
};

int main_calc(int p, int* coins);
int normalize(int &p, int* coins);
int recursive_calc(int p, int* coins, int digit);
void get_coin_names(Coin &c1, Coin &c5, int digit);

int main(int argc, char const* argv[])
{
  int p;
  int coins[6];

  while (true) {
    cin >> p >> coins[N1] >> coins[N5] >> coins[N10] >> coins[N50]
      >> coins[N100] >> coins[N500];
    if (p == 0) break;
    cout << main_calc(p, coins) << endl;
  }

  return 0;
}

int main_calc(int p, int* coins) {
  return normalize(p, coins) + recursive_calc(p, coins, 0);
}

// p縺?譯√↓縺ェ繧九∪縺ァ螟ァ縺阪＞繧ウ繧、繝ウ縺九ｉ菴ソ縺?// 雜ウ繧翫↑縺??蜷医?荳榊ョ?// return: used coins
// p, coins will be modified
int normalize(int &p, int* coins) {
  int _p = p;
  int digit = 2, sum = 0;
  int base = 100;
  int count;

  while (_p != _p % 1000) {
    Coin c1, c5;
    get_coin_names(c1, c5, digit);
    int n1 = coins[c1], n5 = coins[c5];

    // 5縺ョ繧ウ繧、繝ウ繧剃スソ縺?焚
    count = min(_p / (5 * base), n5);
    _p -= 5 * count * base;
    coins[c5] -= count;
    sum += count;

    if(_p == _p % 1000) break;

    // 1縺ョ繧ウ繧、繝ウ繧剃スソ縺?焚
    count = min(_p / base, n1);
    _p -= count * base;
    coins[c1] -= count;
    sum += count;

    if(_p == _p % 1000) break;

    digit--;
    base /= 10;
  }

  p %= 1000;
  return sum;
}

int* copy(int* coins) {
  int* new_coins = new int[6];
  for(int i = 0; i < 6; i++) {
    new_coins[i] = coins[i];
  }
  return new_coins;
}

int changes(int v) {
  if (v < 5) {
    return v;
  }
  return 1 + v - 5;
}

// digit譯√ｈ繧贋ク九?繧ウ繧、繝ウ縺ァp縺ョdigit譯∽サ・荳翫ｒ蜈ィ驛ィ謇輔≧繧ウ繧、繝ウ謨ー
// 繧ウ繧、繝ウ縺瑚カウ繧翫↑縺??蜷医? -1 繧定ソ斐☆
int min_coins(int p, int* coins, int digit) {
  digit -= 1;
  int base = 1;
  for(int i = 0; i < digit; i++) base *= 10;
  int count, sum = 0;

  while (true) {
    if (digit < 0) return -1;

    Coin c1, c5;
    get_coin_names(c1, c5, digit);
    int n1 = coins[c1], n5 = coins[c5];

    count = min(p / (5 * base), n5);
    p -= (5 * base) * count;
    sum += count;

    if(p == p % 1000) break;

    count = min(p / base, n1);
    p -= base * count;
    sum += count;

    if(p == p % 1000) break;

    base /= 10;
    digit--;
  }

  return sum;
}

// digit譯√ｈ繧贋ク翫↓縺、縺?※蜀榊クー縺ァ繧ウ繧、繝ウ謨ー繧呈アゅａ繧?int recursive_calc(int p, int* coins, int digit) {
  //cout << "p: " << p << ", digit: " << digit << endl;
  if (digit == 3) {
    if (p / 1000 != 0) {
      //cout << "500c: " << coins[N500] << endl;
      auto r = min_coins(p, coins, 3);
      //cout << "3譯∫岼coins: " << r << endl;
      return r;
    }
    return 0;
  }

  Coin c1, c5;
  get_coin_names(c1, c5, digit);
  int n1 = coins[c1], n5 = coins[c5];

  // 10^(digit)縺ァ蜑イ繧?  int v = p;
  for(int i = 0; i < digit; i++) v = v / 10;

  // digit譯∫岼縺?¢蜿悶ｊ蜃コ縺?  v %= 10;

  if (v == 0) {
    auto new_coins = copy(coins);
    return recursive_calc(p, new_coins, digit + 1);
  }


  // 謇輔＞譁ケ
  // A: c1#v
  // B: c5#1
  // C: c5#1, c1#(5-v)
  // D: c5#2
  // E: c10繧医ｊ螟ァ縺阪＞繧?▽
  // F: c1繧医ｊ蟆上＆縺?ｄ縺、

  vector<int> amounts;

  // A
  if (v <= n1) {
    int* new_coins = copy(coins);
    new_coins[c1] -= v;
    int amount = v;
    int r = recursive_calc(p, new_coins, digit + 1);

    if (r != -1) {
      amounts.push_back(amount + r);
    }

    delete new_coins;
  }

  // B
  if (v <= 5 && n5 >= 1) {
    int* new_coins = copy(coins);
    new_coins[c5] -= 1;
    int amount = 1 + changes(5 - v);
    int r = recursive_calc(p, new_coins, digit + 1); 

    if (r != -1) {
      amounts.push_back(amount + r);
    }

    delete new_coins;
  }

  // C
  if (v > 5 && n5 >= 1 && c1 >= (5 - v)) {
    int* new_coins = copy(coins);
    new_coins[c5] -= 1;
    new_coins[c1] -= 10 - v;
    int amount = 1 + 10 - v;
    int r = recursive_calc(p, new_coins, digit + 1);

    if (r != -1) {
      amounts.push_back(amount + r);
    }

    delete new_coins;
  }

  // D
  if (n5 >= 2) {
    int* new_coins = copy(coins);
    new_coins[c5] -= 2;
    int amount = 2 + changes(10 - v);
    int r = recursive_calc(p, new_coins, digit + 1);

    if (r != -1) {
      amounts.push_back(amount + r);
    }

    delete new_coins;
  }

  // E
  {
    // p縺ョ1譯∽ク翫↓1雜ウ縺?    int d = 1;
    for(int i = 0; i < digit + 1; i++) d *= 10;
    int new_p = p + d;
    int amount = changes(10 - v);
    int r = recursive_calc(new_p, coins, digit + 1);

    if (r != -1) amounts.push_back(amount + r);
  }

  // F
  if (amounts.empty()) {
    int amount = min_coins(p, coins, digit);
    if (amount != -1) amounts.push_back(amount);
  }

  // 髮?ィ?  if (amounts.empty()) return -1;

  return *min_element(amounts.begin(), amounts.end());
}

void get_coin_names(Coin &c1, Coin &c5, int digit) {
  switch (digit) {
    case 0:
             c1 = N1;
             c5 = N5;
             break;
    case 1:
             c1 = N10;
             c5 = N50;
             break;
    case 2:
             c1 = N100;
             c5 = N500;
             break;
  }
}

Compiler output:
a.cc:124:3: error: expected unqualified-id before 'if'
  124 |   if (digit == 3) {
      |   ^~
a.cc:135:17: error: expected constructor, destructor, or type conversion before '(' token
  135 |   get_coin_names(c1, c5, digit);
      |                 ^
a.cc:136:12: error: 'coins' was not declared in this scope
  136 |   int n1 = coins[c1], n5 = coins[c5];
      |            ^~~~~
a.cc:139:3: error: expected unqualified-id before 'for'
  139 |   for(int i = 0; i < digit; i++) v = v / 10;
      |   ^~~
a.cc:139:18: error: 'i' does not name a type
  139 |   for(int i = 0; i < digit; i++) v = v / 10;
      |                  ^
a.cc:139:29: error: 'i' does not name a type
  139 |   for(int i = 0; i < digit; i++) v = v / 10;
      |                             ^
a.cc:143:3: error: expected unqualified-id before 'if'
  143 |   if (v == 0) {
      |   ^~
a.cc:160:3: error: expected unqualified-id before 'if'
  160 |   if (v <= n1) {
      |   ^~
a.cc:174:3: error: expected unqualified-id before 'if'
  174 |   if (v <= 5 && n5 >= 1) {
      |   ^~
a.cc:188:3: error: expected unqualified-id before 'if'
  188 |   if (v > 5 && n5 >= 1 && c1 >= (5 - v)) {
      |   ^~
a.cc:203:3: error: expected unqualified-id before 'if'
  203 |   if (n5 >= 2) {
      |   ^~
a.cc:217:3: error: expected unqualified-id before '{' token
  217 |   {
      |   ^
a.cc:228:3: error: expected unqualified-id before 'if'
  228 |   if (amounts.empty()) {
      |   ^~
a.cc:235:3: error: expected unqualified-id before 'return'
  235 |   return *min_element(amounts.begin(), amounts.end());
      |   ^~~~~~
a.cc:236:1: error: expected declaration before '}' token
  236 | }
      | ^