Guest User

Untitled

a guest
May 29th, 2019
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Solution{
  6.     FastReader in;
  7.     PrintWriter out;
  8.     Helper_class h;
  9.     public static void main(String[] args) throws java.lang.Exception{
  10.         new Solution().run();
  11.     }
  12.     void run() throws Exception{
  13.         in=new FastReader(System.in);
  14.         out = new PrintWriter(System.out);
  15.         h = new Helper_class();
  16.         int t = h.ni();
  17.         for(int i = 1; i <= t; i++)
  18.             solve(i);
  19.         out.flush();
  20.         out.close();
  21.     }
  22.     TreeMap<Long, Integer> tmap = new TreeMap<Long, Integer>();
  23.     int n, m;
  24.     boolean check(long threshold){
  25.         int cnt = 0;
  26.         long curr = threshold;
  27.         TreeMap<Long, Integer> map = new TreeMap<Long, Integer>();
  28.         for(Long hola : tmap.keySet()) {
  29.             map.put(hola,  tmap.get(hola));
  30.         }
  31.         while(map.size() > 0){
  32.             Long just_smaller = map.floorKey(curr);
  33.             if(just_smaller == null){
  34.                 cnt++;
  35.                 curr = threshold;
  36.                 continue;
  37.             }
  38.             int freq = map.get(just_smaller);
  39.             if(freq == 1)
  40.                 map.remove(just_smaller);
  41.             else
  42.                 map.put(just_smaller, --freq);
  43.             curr -= just_smaller;
  44.         }
  45.         cnt++;
  46.         if(cnt <= m)
  47.             return true;
  48.         else
  49.             return false;
  50.     }
  51.     void solve(int t){
  52.         n = h.ni();
  53.         long[] arr = new long[n];
  54.         int i = 0;
  55.         long max = 0, sum = 0;
  56.         for(i = 0; i < n; i++){
  57.             arr[i] = h.nl();
  58.             max = Math.max(max, arr[i]);
  59.             sum += arr[i];
  60.             Integer x = tmap.get(arr[i]);
  61.             if(x == null)
  62.                 tmap.put(arr[i], 1);
  63.             else
  64.                 tmap.put(arr[i], ++x);
  65.         }
  66.         m = h.ni();
  67.         long lo = max, hi = sum;
  68.         while(lo <= hi){
  69.             long mid = (lo + hi) >> 1;
  70.             if(check(mid))
  71.                 hi = mid - 1;
  72.             else
  73.                 lo = mid + 1;
  74.         }
  75.         h.pn(hi + 1);
  76.     }
  77.    
  78.    
  79.     class Helper_class{
  80.        
  81.         void pn(Object o){out.println(o);}
  82.         int ni(){return Integer.parseInt(in.next());}
  83.         long nl(){return Long.parseLong(in.next());}
  84.     }
  85.  
  86.     class FastReader{
  87.         private InputStream stream;
  88.         private byte[] buf = new byte[1024];
  89.         private int curChar;
  90.         private int numChars;
  91.  
  92.         public FastReader(InputStream stream) {
  93.             this.stream = stream;
  94.         }
  95.  
  96.         public int read() {
  97.             if (numChars == -1)
  98.                 throw new UnknownError();
  99.             if (curChar >= numChars) {
  100.                 curChar = 0;
  101.                 try {
  102.                     numChars = stream.read(buf);
  103.                 } catch (IOException e) {
  104.                     throw new UnknownError();
  105.                 }
  106.                 if (numChars <= 0)
  107.                     return -1;
  108.             }
  109.             return buf[curChar++];
  110.         }
  111.  
  112.         public int peek() {
  113.             if (numChars == -1)
  114.                 return -1;
  115.             if (curChar >= numChars) {
  116.                 curChar = 0;
  117.                 try {
  118.                     numChars = stream.read(buf);
  119.                 } catch (IOException e) {
  120.                     return -1;
  121.                 }
  122.                 if (numChars <= 0)
  123.                     return -1;
  124.             }
  125.             return buf[curChar];
  126.         }
  127.  
  128.         public void skip(int x) {
  129.             while (x-- > 0)
  130.                 read();
  131.         }
  132.  
  133.         public int nextInt() {
  134.             return Integer.parseInt(next());
  135.         }
  136.  
  137.         public long nextLong() {
  138.             return Long.parseLong(next());
  139.         }
  140.  
  141.         public String nextString() {
  142.             return next();
  143.         }
  144.  
  145.         public String next() {
  146.             int c = read();
  147.             while (isSpaceChar(c))
  148.                 c = read();
  149.             StringBuffer res = new StringBuffer();
  150.             do {
  151.                 res.appendCodePoint(c);
  152.                 c = read();
  153.             } while (!isSpaceChar(c));
  154.  
  155.             return res.toString();
  156.         }
  157.  
  158.         public String nextLine() {
  159.             StringBuffer buf = new StringBuffer();
  160.             int c = read();
  161.             while (c != '\n' && c != -1) {
  162.                 if (c != '\r')
  163.                     buf.appendCodePoint(c);
  164.                 c = read();
  165.             }
  166.             return buf.toString();
  167.         }
  168.  
  169.         public double nextDouble() {
  170.             int c = read();
  171.             while (isSpaceChar(c))
  172.                 c = read();
  173.             int sgn = 1;
  174.             if (c == '-') {
  175.                 sgn = -1;
  176.                 c = read();
  177.             }
  178.             double res = 0;
  179.             while (!isSpaceChar(c) && c != '.') {
  180.                 if (c == 'e' || c == 'E')
  181.                     return res * Math.pow(10, nextInt());
  182.                 if (c < '0' || c > '9')
  183.                     throw new InputMismatchException();
  184.                 res *= 10;
  185.                 res += c - '0';
  186.                 c = read();
  187.             }
  188.             if (c == '.') {
  189.                 c = read();
  190.                 double m = 1;
  191.                 while (!isSpaceChar(c)) {
  192.                     if (c == 'e' || c == 'E')
  193.                         return res * Math.pow(10, nextInt());
  194.                     if (c < '0' || c > '9')
  195.                         throw new InputMismatchException();
  196.                     m /= 10;
  197.                     res += (c - '0') * m;
  198.                     c = read();
  199.                 }
  200.             }
  201.             return res * sgn;
  202.         }
  203.  
  204.         public boolean hasNext() {
  205.             int value;
  206.             while (isSpaceChar(value = peek()) && value != -1)
  207.                 read();
  208.             return value != -1;
  209.         }
  210.  
  211.         private boolean isSpaceChar(int c) {
  212.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  213.         }
  214.     }
  215. }
Add Comment
Please, Sign In to add comment