01背包问题,不多说

package Interview.woderfulLife;

import java.util.Scanner;

/**
 * 01背包问题
 */
public class pickGift {
    //输入
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int nums = scanner.nextInt();
        int bag = scanner.nextInt();

        int[] weight = new int[nums+1];
        int[] vals = new int[nums+1];

        for (int i = 1; i <= nums; i++) {
            weight[i] = scanner.nextInt();
        }

        for (int i = 1; i <= nums; i++) {
            vals[i] = scanner.nextInt();
        }

        getMax(bag,vals,weight);
    }

    private static void getMax(int bag, int[] vals, int[] weight) {

        int[][] dp = new int[vals.length][bag+1];
        for (int i = 0; i < bag + 1; i++) {
            dp[0][i] = 0;
        }

        for (int i = 0; i < vals.length; i++) {
            dp[i][0] = 0;
        }

        for (int i = 1; i <vals.length; i++) {
            for (int j = 1; j <= bag; j++) {
                if (j>=weight[i]){
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][j-weight[i]]+vals[i]);
                }else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        System.out.println(dp[vals.length-1][bag]);
    }
}