Skip to content

Commit e329d03

Browse files
committed
[Week05] BOJ 1781: 컵라면
1 parent 482c4fe commit e329d03

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package week05.BOJ_1781_컵라면;
2+
import java.util.*;
3+
import java.lang.*;
4+
import java.io.*;
5+
6+
class BOJ1781 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
int N = Integer.parseInt(br.readLine());
10+
int[][] works = new int[N][2]; //데드라인, 컵라면 수
11+
for (int i = 0; i < N; i++) {
12+
StringTokenizer st = new StringTokenizer(br.readLine());
13+
works[i][0] = Integer.parseInt(st.nextToken());
14+
works[i][1] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
Arrays.sort(works, (o1, o2) -> {
18+
if (o1[0] == o2[0]) return o2[1] - o1[1];
19+
return o1[0] - o2[0];
20+
});
21+
22+
PriorityQueue<Integer> pq = new PriorityQueue<>(); //푼 문제의 컵라면 수 저장
23+
for (int i = 0; i < N; i++) {
24+
25+
//pq 사이즈 == 푼 문제 수 == 사용한 시간과 데드라인 시간 비교
26+
if (pq.size() < works[i][0]) {
27+
pq.offer(works[i][1]);
28+
} else {
29+
if (pq.peek() < works[i][1]) { //기존에 풀었던 문제의 컵라면 수보다 크면 교체
30+
pq.poll();
31+
pq.offer(works[i][1]);
32+
}
33+
}
34+
}
35+
36+
//출력
37+
int answer = 0;
38+
while (!pq.isEmpty()) {
39+
answer += pq.poll();
40+
}
41+
System.out.println(answer);
42+
}
43+
}

0 commit comments

Comments
 (0)