Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Scanner;
- public class Main{
- public static void main(String[] args) {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- Scanner in = new Scanner(br);
- int casos = in.nextInt();
- while (casos > 0) {
- int nobj = in.nextInt();
- int[] W = new int[nobj + 1];
- int[] V = new int[nobj + 1];
- for (int i = 0; i < nobj; i++) {
- V[i + 1] = in.nextInt();
- W[i + 1] = in.nextInt();
- }
- int familiar = in.nextInt();
- int total = 0;
- while (familiar > 0) {
- int limite = in.nextInt();
- int[] peso = new int[limite + 1];
- for (int j = 0; j < limite; j++) {
- peso[j + 1] = j + 1;
- }
- int[][] matrix = new int[W.length][peso.length];
- total = total + KnapSack(W.length, peso.length, W, V, matrix);
- familiar--;
- }
- System.out.println(total);
- casos--;
- }
- }
- static int KnapSack(int n1, int n2, int[] W, int[] V, int[][] matrix) {
- for (int i = 1; i < n1; i++) {
- for (int j = 1; j < n2; j++) {
- if (W[i] <= j) {
- matrix[i][j] = Math.max(
- matrix[i - 1][j], V[i] + matrix[i - 1][j - W[i]]);
- } else
- matrix[i][j] = matrix[i - 1][j];
- }
- }
- return matrix[n1-1][n2-1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement