mubtasim91

Gambling

Apr 12th, 2019
586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. import java.io.PrintWriter;
  2. import java.util.Collections;
  3. import java.util.PriorityQueue;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Main {
  8.  
  9.   public static void main(String[] args) {
  10.     Scanner sc = new Scanner(System.in);
  11.     PrintWriter out = new PrintWriter(System.out);
  12.     int n = sc.nextInt();
  13.     PriorityQueue<Integer> pq1 = new PriorityQueue(Collections.reverseOrder());
  14.     PriorityQueue<Integer> pq2 = new PriorityQueue(Collections.reverseOrder());
  15.     long ta = 0;
  16.     long tb = 0;
  17.     for(int i = 0; i < n; i++)
  18.       pq1.add(sc.nextInt());
  19.     for(int i = 0; i < n; i++)
  20.       pq2.add(sc.nextInt());
  21.      
  22.     int cnt = 0;
  23.      
  24.     while(!pq1.isEmpty() && !pq2.isEmpty()){
  25.       int a = pq1.peek();
  26.       int b = pq2.peek();
  27.       if(b>=a){
  28.         pq2.poll();
  29.       } else {
  30.         ta += pq1.poll();
  31.       }
  32.      
  33.       cnt++;
  34.      
  35.       if(!pq1.isEmpty() && !pq2.isEmpty()){
  36.         a = pq1.peek();
  37.         b = pq2.peek();
  38.         if(a>=b){
  39.           pq1.poll();
  40.         } else {
  41.           tb += pq2.poll();
  42.         }
  43.         cnt++;
  44.       }
  45.     }
  46.    
  47.     if(cnt%2==0){
  48.       if(!pq1.isEmpty()){
  49.         while(!pq1.isEmpty()){
  50.           ta += pq1.poll();
  51.           if(!pq1.isEmpty()) pq1.poll();
  52.         }
  53.       } else {
  54.         while(!pq2.isEmpty()){
  55.           pq2.poll();
  56.           if(!pq2.isEmpty())
  57.             tb += pq2.poll();
  58.         }
  59.       }
  60.     } else {
  61.       if(!pq2.isEmpty()){
  62.         while(!pq2.isEmpty()){
  63.           tb += pq2.poll();
  64.           if(!pq2.isEmpty()) pq2.poll();
  65.         }
  66.       } else {
  67.         while(!pq1.isEmpty()){
  68.           pq1.poll();
  69.           if(!pq1.isEmpty())
  70.             ta += pq1.poll();
  71.         }
  72.       }
  73.     }
  74.    
  75.     out.println(ta-tb);
  76.     out.close();
  77.   }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment