Skip to content

Commit 98230b1

Browse files
committed
백준 1202번 보석 도둑 v2
문제 링크: https://www.acmicpc.net/problem/1202
1 parent 5b5a32b commit 98230b1

File tree

6 files changed

+75
-0
lines changed

6 files changed

+75
-0
lines changed
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import sys
2+
import heapq
3+
4+
5+
def solve():
6+
input = sys.stdin.readline
7+
N, K = map(int, input().strip().split())
8+
MVs = []
9+
for _ in range(N):
10+
Mi, Vi = map(int, input().strip().split())
11+
heapq.heappush(MVs, [Mi, Vi])
12+
13+
bags = [int(input().strip()) for _ in range(K)]
14+
bags.sort()
15+
16+
answer = 0
17+
tmp = []
18+
19+
for bag in bags:
20+
while MVs:
21+
Mmin, Vmax = heapq.heappop(MVs)
22+
23+
if bag >= Mmin:
24+
heapq.heappush(tmp, -Vmax)
25+
else:
26+
heapq.heappush(MVs, [Mmin, Vmax])
27+
break
28+
29+
if tmp:
30+
Vminus = heapq.heappop(tmp)
31+
answer += -Vminus
32+
33+
print(answer)
34+
35+
36+
if __name__ == '__main__':
37+
solve()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2 1
2+
5 10
3+
100 100
4+
11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
3 2
2+
1 65
3+
5 23
4+
2 99
5+
10
6+
2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
164
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')
24+
25+
def test2_solve(self):
26+
self.test_solve('2')

0 commit comments

Comments
 (0)