import java.io.*; import java.util.StringTokenizer; public class SuperSale { public static void main(String[] args) throws FileNotFoundException { InputReader in = new InputReader(System.in); int T = in.nextInt(); while (T-- != 0) { int N = in.nextInt(); Object[] A = new Object[N]; int sum = 0; for (int n = 0; n < N; n++) { A[n] = new Object(in.nextInt(), in.nextInt()); } int G = in.nextInt(); int MW; int max = 0; for (int g = 0; g < G; g++) { MW = in.nextInt(); int[][] memo = new int[A.length + 1][31]; for (int i = 1; i <= A.length; i++) { for (int j = 0; j <= MW; j++) { if (j + A[i - 1].weight <= MW) { memo[i][j + A[i - 1].weight] = Math.max(memo[i - 1][j] + A[i - 1].price, memo[i][j]); } memo[i][j] = Math.max(memo[i][j], memo[i - 1][j]); } } int localMax = 0; for (int j = 0; j <= MW; j++) { localMax = Math.max(localMax, memo[A.length][j]); } max += localMax; } System.out.println(max); } } public static class Object { public int price; public int weight; public Object(int price, int weight) { this.price = price; this.weight = weight; } } public static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
Sample Input:
2 3 72 17 44 23 31 24 1 26 6 64 26 85 22 52 4 99 18 39 13 54 9 4 23 20 20 26
Sample Output:
72 514