1+ """
2+ # [BOJ] #1202. ๋ณด์ ๋๋
3+ ์ ํ - ๊ทธ๋ฆฌ๋ , ์ฐ์ ํ , ๋ฐฑ์ค
4+
5+ - ๊ฐ๋ฐฉ 1๊ฐ์ ๋ณด์ ํ๊ฐ
6+ """
7+ import sys
8+ from collections import deque
9+
10+ # 1, jewel , bags์ ๊ฐ ์
๋ ฅ ๋ฐ๊ธฐ
11+ total_jewels , total_bags = map (int , sys .stdin .readline ().split ())
12+ jewels = [[] for _ in range (total_jewels )]
13+ bags = [ 0 for _ in range (total_bags )]
14+
15+ for i in range (total_jewels ) :
16+ jewels [i ] = list (map (int , sys .stdin .readline ().split ()))
17+
18+ for j in range (total_bags ) :
19+ bags [j ] = int (sys .stdin .readline ())
20+
21+
22+
23+ #2. ์ ๋ ฌ
24+ #jewel ์ ๋น์ผ ๊ฐ๊ฒฉ-> ๊ฐ๋ฒผ์ด ๋ฌด๊ฒ ์์ผ๋ก ์ ๋ ฌ
25+ # bags ์ ๋ฌด๊ฒ๊ฐ ๋ฌด๊ฑฐ์ด ์์ผ๋ก ์ ๋ ฌ
26+ jewels = sorted (jewels , key = lambda x : ( - x [1 ] , x [0 ]))
27+ bags = sorted (bags ) # max = -1
28+
29+
30+
31+ # #3. ๊ทธ๋ฆฌ๋ - ํ์ฌ ๊ฐ์ฅ ๋น์ผ jewel ์์ผ๋ก ๊ฐ์ฅ ์์ ๊ฐ๋ฐฉ์์ ๋ฃ๊ธฐ
32+ prices = []
33+
34+ for i in range (total_jewels ) :
35+ jewel_weight , jewel_price = jewels [i ]
36+ # ํ์ฌ jewel ์ ๋ฌด๊ฒ๊ฐ ์ต๋ bags ๋ณด๋ค ๋ฌด๊ฑฐ์ฐ๋ฉด => ๋
37+ if jewel_weight > max (bags ) :
38+ continue
39+ else : # ์๋๋ฉด => ๊ฐ๋ฐฉ์ ๋ฐ๋ก ๋ฃ๊ธฐ
40+ # jewel์ ๋ฌด๊ฒ์ ๊ฐ์ฅ ๊ฐ๊น์ด fitํ ์ต์ ์ bags ์ฐพ๊ธฐ
41+ # diff = 1000000000 ; fit_idx = -1
42+ # for k in range(len(bags)) :
43+ # curr_diff = bags[k]-jewel_weight
44+ # if curr_diff > 0 and diff > curr_diff :
45+ # diff = curr_diff ; fit_idx= k
46+
47+ for k in range (len (bags )) :
48+ if bags [k ] >= jewel_weight : # ํ jewle weight๋ฅผ ๊ฐ๋น ๊ฐ๋ฅํ ์ต์ ๊ฐ๋ฐฉ
49+ prices .append (jewel_price )
50+ bags .pop (k ) # ๋๊ฐ๊ธฐ
51+ break
52+ if len (bags ) == 0 : # ๋น์ด์๋ค
53+ break # ์ข
๋ฃ
54+ # print(f"current bags {bags} : jewel {jewel_price} / {jewel_weight}")
55+ # print(prices)
56+ # print(f"price : {sum(prices)}")
57+ print (sum (prices ))
0 commit comments