动态规划 - 杭电acm 2602

http://acm.hdu.edu.cn/showproblem.php?pid=2602

import java.util.Scanner;

//http://acm.hdu.edu.cn/showproblem.php?pid=2602
public class Main {

    static Scanner input = new Scanner(System.in);
    static {
    }

    public static void main(String[] args) {
        int count = input.nextInt();
        for (int i = 0; i < count; i++) {
            int n = input.nextInt();
            int max = input.nextInt();
            int[] values = new int[n];
            int[] volumns = new int[n];
            for (int j = 0; j < n; j++) {
                values[j] = input.nextInt();
            }
            for (int j = 0; j < n; j++) {
                volumns[j] = input.nextInt();
            }
            int[] result = new int[max + 1];
            for (int j = 0; j < n; j++) {
                int value = values[j];
                int volumn = volumns[j];
                for (int j2 = result.length - 1; j2 > -1; j2--) {
                    if (result[j2] != 0 && j2 + volumns[j] <= max && result[j2 + volumns[j]] < result[j2] + values[j]) {
                        result[j2 + volumns[j]] = result[j2] + values[j];
                    }
                }
                if (volumn <= max && result[volumn] < value) {
                    result[volumn] = value;
                }

            }

            max = 0;
            for (int j : result) {
                if (max < j) {
                    max = j;
                }
            }
            System.out.println(max);
        }
    }

}

你可能感兴趣的:(动态规划 - 杭电acm 2602)