์ฝ๋ฉํ
์คํธ/TIL
[99ํด๋ฝ] 18์ผ์ฐจ ๋ฌธ์ : ํฌ๋ฆฌ์ค๋ง์ค ์ ๋ฌผ
moon101
2025. 2. 12. 23:09
๋ฐฑ์ค ํฌ๋ฆฌ์ค๋ง์ค ์ ๋ฌผ ๋ฌธ์ ๋ฅผ ์ฐ์ ์์ ํ๋ฅผ ์ฌ์ฉํด์ ํ์๋ค. ๋งจ ์ฒ์ ์ฐ์ ์์ ํ๋ฅผ ์ ํ์ ๋ ๋๊ฒ ์ด๋ ต๋ค๊ณ ์๊ฐํ๋๋ฐ ์ด์ ๋ง์ด ์ต์ํด ์ง ๊ฒ ๊ฐ๋ค. ์์ง ๋น๊ธฐ๋ ๋ ๋ฒจ์ด์ง๋ง ๊ณ์ ํ๋ค๋ณด๋ฉด ๋ฆฌํธ์ฝ๋ ๋ฏธ๋์์ ์ฝ๊ฒ ํ ์ ์๋ ๋ ์ด ์ค๊ฒ ์ง.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int n = Integer.parseInt(br.readLine());
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
if (a == 0) {
if (pq.isEmpty()) {
sb.append("-1\n");
} else {
sb.append(pq.poll()).append("\n");
}
} else {
for (int j = 0; j < a; j++) {
pq.offer(Integer.parseInt(st.nextToken()));
}
}
}
System.out.print(sb);
}
}