-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaekjoon_1202.py
More file actions
82 lines (62 loc) · 1.49 KB
/
Copy pathbaekjoon_1202.py
File metadata and controls
82 lines (62 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import heapq
import sys
input = sys.stdin.readline
N, K = map(int, input().split())
jewel = []
K_list = []
K_heap = []
# 시간복잡도 O(N) -> 통과
for _ in range(N):
M, V = map(int, input().split())
jewel.append((M, V))
# 시간복잡도 O(K) -> 통과
for i in range(K):
C = int(input())
K_list.append(C)
# 시간복잡도 O(NlogN) -> 통과
jewel = sorted(jewel, key=lambda x:(x[0], -x[1]))
# 시간복잡도 O(KlogK) -> 통과
K_list = sorted(K_list, key=lambda x:x) # 힙을 사용해서 필요X
## 시간 초과 부분
"""
result = 0
for (M, V) in jewel:
for i, C in enumerate(K_list):
# print(K_list)
if M <= C:
result += V
K_list.pop(i)
break
"""
# 이진 탐색
"""
def binary_search(find, data):
start = 0
end = len(data) -1
if end < 0:
return -1
if find > data[-1]:
return -1
while start<=end:
mid = (start+end) // 2
if data[mid] == find:
return mid
elif data[mid] > find:
end = mid -1
else:
start = mid + 1
return start
"""
# 가방에 넣을 수 있는 보석중 가장 가치가 큰것
result = 0
heap = []
for C in K_list:
# if not jewel:
# break
# 일단 가방크기보다 작은 보석들 heap에 담음
while jewel and jewel[0][0] <= C:
heapq.heappush(heap, -jewel[0][1])
heapq.heappop(jewel)
if heap:
result -= heapq.heappop(heap)
print(result)