Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.PrintWriter;
- import java.util.Collections;
- import java.util.PriorityQueue;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- PrintWriter out = new PrintWriter(System.out);
- int n = sc.nextInt();
- PriorityQueue<Integer> pq1 = new PriorityQueue(Collections.reverseOrder());
- PriorityQueue<Integer> pq2 = new PriorityQueue(Collections.reverseOrder());
- long ta = 0;
- long tb = 0;
- for(int i = 0; i < n; i++)
- pq1.add(sc.nextInt());
- for(int i = 0; i < n; i++)
- pq2.add(sc.nextInt());
- int cnt = 0;
- while(!pq1.isEmpty() && !pq2.isEmpty()){
- int a = pq1.peek();
- int b = pq2.peek();
- if(b>=a){
- pq2.poll();
- } else {
- ta += pq1.poll();
- }
- cnt++;
- if(!pq1.isEmpty() && !pq2.isEmpty()){
- a = pq1.peek();
- b = pq2.peek();
- if(a>=b){
- pq1.poll();
- } else {
- tb += pq2.poll();
- }
- cnt++;
- }
- }
- if(cnt%2==0){
- if(!pq1.isEmpty()){
- while(!pq1.isEmpty()){
- ta += pq1.poll();
- if(!pq1.isEmpty()) pq1.poll();
- }
- } else {
- while(!pq2.isEmpty()){
- pq2.poll();
- if(!pq2.isEmpty())
- tb += pq2.poll();
- }
- }
- } else {
- if(!pq2.isEmpty()){
- while(!pq2.isEmpty()){
- tb += pq2.poll();
- if(!pq2.isEmpty()) pq2.poll();
- }
- } else {
- while(!pq1.isEmpty()){
- pq1.poll();
- if(!pq1.isEmpty())
- ta += pq1.poll();
- }
- }
- }
- out.println(ta-tb);
- out.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment