-
Notifications
You must be signed in to change notification settings - Fork 0
/
D.py
45 lines (35 loc) · 846 Bytes
/
D.py
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
from collections import Counter
from collections import deque
n, m = map(int, input().split())
a = list(map(int, input().split()))
bc = [list(map(int, input().split())) for _ in range(m)]
a_count = Counter(a)
a = [[key, value] for value, key in a_count.items()]
a.sort(reverse=True, key=lambda x: x[1])
a = deque(a)
bc.sort(reverse=True, key=lambda x: x[1])
bc = deque(bc)
ans = 0
count = 0
while count<n or (bc and a):
if bc:
bc_n, bc_v = bc[0]
else:
bc_n, bc_v = 0, 0
if a:
a_n, a_v = a[0]
else:
a_n, a_v = 0, 0
if a_v >= bc_v:
a.popleft()
if n-count<a_n:
a_n = n-count
count += a_n
ans += a_n*a_v
else:
bc.popleft()
if n-count<bc_n:
bc_n = n-count
count += bc_n
ans += bc_n*bc_v
print(ans)