본문 바로가기
코딩 문제 풀이/백준

[백준] 자바 문제 풀이 13904 : 골드3

by 코딩하는 랄로 2023. 9. 20.
728x90
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {
	static class Hw implements Comparable<Hw>{
		int d;
		int w;
		public Hw(int d, int w) {
			this.d = d;
			this.w = w;
		}
		@Override
		public int compareTo(Hw hw) {
			return hw.w < w ? 1 : (hw.w > w ? -1 : 0);
		}
	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		
		StringTokenizer str;
		List<Hw> hlist = new ArrayList<>();
		for(int i = 0; i < n; i++) {
			str = new StringTokenizer(br.readLine(), " ");
			hlist.add(new Hw(Integer.parseInt(str.nextToken()), Integer.parseInt(str.nextToken())));
		}
		Collections.sort(hlist, new Comparator<Hw>() {
			@Override
			public int compare(Hw hw1, Hw hw2) {
				return hw1.d >= hw2.d ? 1 : -1;
			}
		});

		PriorityQueue<Hw> queue = new PriorityQueue<>();
		queue.add(hlist.remove(0));
		
		while(!hlist.isEmpty()) {
			Hw hw = hlist.remove(0);
			
			if(hw.d == queue.size()) {
				if(hw.w > queue.peek().w) {
					queue.poll();
					queue.add(hw);
				}
			}
			else {
				queue.add(hw);
			}
		}
		
		int sum = 0;
		while(!queue.isEmpty()) {
			sum += queue.poll().w;
		}
		System.out.println(sum);
	}

}
728x90