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);
}

// 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];

    count = min(_p / (5 * base), n5);
    _p -= 5 * count * base;
    coins[c5] -= count;
    sum += count;

    if(_p == _p % 1000) break;

    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;
}

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;
}

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);
      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;

  v %= 10;

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


  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
  {
    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: In function 'int recursive_calc(int, int*, int)':
a.cc:134:34: error: 'v' was not declared in this scope
  134 |   for(int i = 0; i < digit; i++) v = v / 10;
      |                                  ^
a.cc:136:3: error: 'v' was not declared in this scope
  136 |   v %= 10;
      |   ^