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

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

by 코딩하는 랄로 2023. 9. 23.
728x90

BOJ 2109 풀이 코드

import java.io.*;
import java.util.*;

public class Main {
	static class Lecture {
		int cost;
		int day;

		Lecture(int cost, int day) {
			this.cost = cost;
			this.day = day;
		}
	}

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int n = Integer.parseInt(br.readLine());
		PriorityQueue<Lecture> deadline = new PriorityQueue<>((o1, o2) -> o1.day - o2.day);

		for (int i = 0; i < n; i++) {
			StringTokenizer str = new StringTokenizer(br.readLine(), " ");
			deadline.add(new Lecture(Integer.parseInt(str.nextToken()), Integer.parseInt(str.nextToken())));
		}

		PriorityQueue<Lecture> rst = new PriorityQueue<>((o1, o2) -> o1.cost - o2.cost);
		if (!deadline.isEmpty()) {
			rst.add(deadline.poll());

			while (!deadline.isEmpty()) {
				Lecture l = deadline.poll();

				if (l.day == rst.size()) {
					if (l.cost > rst.peek().cost) {
						rst.poll();
						rst.add(l);
					}
				} else {
					rst.add(l);
				}

			}
		}
		int total = 0;
		while (!rst.isEmpty()) {
			total += rst.poll().cost;
		}
		bw.write(total + "\n");
		bw.flush();
		bw.close();
		br.close();
	}

}
728x90