钱从大到小一枚一枚加起来就行了,直到等于A为止:

import java.util.*;
public class Main {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        int[] C = new int[6], v = {1, 5, 10, 50, 100, 500};
        for(int i = 0; i < 6; ++i) C[i] = sc.nextInt();
        int A = sc.nextInt(), p = 5, r = 0;    //货币指针p初始在500元的位置,r是硬币枚数
        while(A > 0){
            if(p < 0) break;
            if(C[p] == 0 || A < v[p]){    //没有当前种类的硬币,或者单面值已超过A,就看下一个小面值
                p--;
                continue;
            }
            A -= v[p];
            C[p]--;    //每用一个硬币,就减少一个
            r++;
            if(A == 0){    //正好凑够了A这么多钱
                System.out.println(r);
                return;
            }
        }
        System.out.println("NOWAY");
    }
}