java实现 wa求助(过了三个点)
查看原帖
java实现 wa求助(过了三个点)
342231
NEURookie楼主2022/5/4 21:38
import java.io.*;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int n = nextInt();
        int m = nextInt();
        int[] k = new int[n];
        int[] c = new int[n];
        for (int i = 0; i < n; i++) {
            k[i] = nextInt();
        }
        int totalMoney = 0;
        for (int i = 0; i < n; i++) {
            c[i] = nextInt();
            totalMoney += c[i] * k[i];
        }
        long[] dpArray = new long[totalMoney + 1];
        //填充为1
        Arrays.fill(dpArray, 1);
        for (int i = 0; i < n; i++) {
            //倒序枚举价格
            for (int j = totalMoney; j >= c[i] ; j--) {
                //遍历所有物品数量
                for (int l = 1; l <= k[i] && l * c[i] <= j; l++) {
                    long newVal = dpArray[j - l * c[i]] * l;
                    if(newVal > dpArray[j] && dpArray[j]<m){
                        dpArray[j] = newVal;
                    }
                }
            }
        }
        //遍历寻找解
        for (int i = 1; i <= totalMoney; i++) {
            if(dpArray[i] >= m){
                out.println(i);
                break;
            }
        }
        out.close();
    }

    static StreamTokenizer stk = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static int nextInt() {
        try {
            stk.nextToken();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (int) stk.nval;
    }
}
2022/5/4 21:38
加载中...