Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class RPLB{
  5.     public static void main(String[] args) throws Exception{
  6.         Parser sc = new Parser(System.in);
  7.  
  8.         int T = sc.nextInt();
  9.         StringBuilder string = new StringBuilder();
  10.         for(int t = 1; t <= T; t++){
  11.             int N = sc.nextInt();
  12.             int MAX = sc.nextInt();
  13.  
  14.             int[] bush = new int[N];
  15.  
  16.             for(int i = 0; i < N; i++){
  17.                 bush[i] = sc.nextInt();
  18.             }
  19.             int[][] dp = new int[N+5][MAX+5];
  20.  
  21.             for(int i = 1; i <= MAX; i++){
  22.                 if(bush[0] <= i){
  23.                     dp[0][i] = bush[0];
  24.                 }
  25.             }
  26.  
  27.             for(int row = 1; row < N; row++){
  28.                 for(int col = 0; col <= MAX; col++){
  29.  
  30.                     if(col - bush[row] >= 0 && row >= 2){
  31.                         if(dp[row-2][col-bush[row]] + bush[row] <= col){
  32.                             dp[row][col] = Math.max(dp[row-2][col-bush[row]] + bush[row], dp[row-1][col]);
  33.                         }
  34.                     }
  35.                     else{
  36.                         if(bush[row] <= col){
  37.                             dp[row][col] = Math.max(dp[row-1][col], bush[row]);
  38.                         }
  39.                         else{
  40.                             dp[row][col] = dp[row-1][col];
  41.                         }
  42.                     }
  43.                 }
  44.             }
  45.             string.append("Scenario #" + t + ": " + dp[N-1][MAX] +"\n");
  46.  
  47.         }
  48.         System.out.println(string);
  49.     }
  50.  
  51.     public static void printArray(int[][] array){
  52.         for(int i = 0; i < array.length; i++){
  53.             for(int j = 10; j < array[0].length; j += 10){
  54.                 System.out.print(array[i][j] + " ");
  55.             }
  56.             System.out.println();
  57.         }
  58.     }
  59.  
  60.     public static class Parser{
  61.        final private int BUFFER_SIZE = 1 << 16;
  62.  
  63.        private DataInputStream din;
  64.        private byte[] buffer;
  65.        private int bufferPointer, bytesRead;
  66.  
  67.        public Parser(InputStream in){
  68.           din = new DataInputStream(in);
  69.           buffer = new byte[BUFFER_SIZE];
  70.           bufferPointer = bytesRead = 0;
  71.        }
  72.  
  73.        public int nextInt() throws Exception{
  74.           int ret = 0;
  75.           byte c = read();
  76.           while (c <= ' ') c = read();
  77.           boolean neg = c == '-';
  78.           if (neg) c = read();
  79.           do
  80.           {
  81.              ret = ret * 10 + c - '0';
  82.              c = read();
  83.           } while (c > ' ');
  84.           if (neg) return -ret;
  85.           return ret;
  86.        }
  87.  
  88.        private void fillBuffer() throws Exception{
  89.           bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  90.           if (bytesRead == -1) buffer[0] = -1;
  91.        }
  92.  
  93.        private byte read() throws Exception{
  94.           if (bufferPointer == bytesRead) fillBuffer();
  95.           return buffer[bufferPointer++];
  96.        }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement