program difficult;

const value: array[1..10] of integer = (1,2,5,10,20,50,100,200,500,1000);
var coins: array[1..10] of integer;
    i, price, result: integer;

function payClassic(v: integer): integer;
  var i, m, used: integer;
  begin
    used := 0;
    for i := 10 downto 1 do begin
      if (value[i] <= v) then begin
        m := v div value[i];
        if (m > coins[i]) then m := coins[i];
        used := used + m;
        v := v - m * value[i];
      end;
    end;

    if (v > 0) then used := -1;
    payClassic := used;
  end;

function pay(v: integer): integer;
  var used, min, i, j: integer;
  begin
    min := payClassic(v);

    for i := 1 to 10 do begin
      j := 1;
      while (j <= coins[i]) and (v >= j * value[i]) do begin
        coins[i] := coins[i] - j;
        used := payClassic(v - j * value[i]);
        if (used <> -1) then used := used + j;
        coins[i] := coins[i] + j;

        if (min = -1) or ((used >= 0) and (used < min)) then min := used;
        j := j + 1;
      end;
    end;
    pay := min;
  end;

begin
  for i := 1 to 10 do read(coins[i]);
  read(price);

  result := pay(price);
  writeln(result);
end.


#include <stdio.h>

int coins[10];
int value[10] = {1,2,5,10,20,50,100,200,500,1000};

int payClassic(int v) {
  int i, m, used;

  used = 0;
  for (i = 9; i >= 0; i--) {
      if (value[i] <= v) {
          m = v / value[i];
          if (m > coins[i]) m = coins[i];
          used += m;
          v -= m * value[i];
      }
  }

  if (v > 0) used = -1;
  return used;
}

int pay(int v) {

  int used, min, i, j;

  min = payClassic(v);

  for (i = 0; i < 10; i++) {
      for (j = 1; j <= coins[i] && v >= j * value[i]; j++) {

          coins[i] -= j;
          used = payClassic(v - j * value[i]);
          if (used != -1) used += j;
          coins[i] += j;

          if (min == -1 || (used >= 0 && used < min))
            min = used;
      }
  }
  return min;
}

int main() {
  int i, price, result;

  for (i = 0; i < 10; i++) scanf("%i", &coins[i]);
  scanf("%i", &price);

  result = pay(price);
  printf("%d\n", result);
  return 0;
}
