File tree 3 files changed +105
-0
lines changed
3 files changed +105
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < algorithm>
3
+ #include < vector>
4
+ #include < utility>
5
+
6
+ using namespace std ;
7
+
8
+
9
+ int main (){
10
+ int N, K, M, V, C;
11
+ vector<pair<int , int > > gems;
12
+ vector<int > packs;
13
+
14
+ cin >> N >> K;
15
+ for (int i = 0 ; i < N; ++i){
16
+ cin >> M >> V;
17
+ gems.push_back (make_pair (V, M));
18
+ }
19
+
20
+
21
+ for (int i = 0 ; i < K; ++i){
22
+ cin >> C;
23
+ packs.push_back (C);
24
+ }
25
+
26
+ int result = 0 ;
27
+
28
+ sort (gems.begin (), gems.end (), greater<pair<int , int > >());
29
+ sort (packs.begin (), packs.end ());
30
+
31
+ for (int i = 0 ; i < N; ++i){
32
+ pair<int , int > gem = gems[i];
33
+
34
+ // lowerbound pack 찾기
35
+ // binary search
36
+ vector<int >::iterator low = packs.begin ();
37
+ vector<int >::iterator high = packs.end ();
38
+
39
+ for (vector<int >::iterator it = packs.begin (); it != packs.end (); it++){
40
+ if (*it >= gem.second ){
41
+ result += gem.first ;
42
+ packs.erase (it);
43
+ break ;
44
+ }
45
+ }
46
+
47
+ if (packs.empty ()) break ;
48
+ }
49
+
50
+ cout << result << endl;
51
+ return 0 ;
52
+ }
Original file line number Diff line number Diff line change
1
+ def lowerbound (li , v ):
2
+ if v > li [- 1 ]:
3
+ return None
4
+ if v <= li [0 ]:
5
+ return 0
6
+ low = 0
7
+ high = len (li )
8
+
9
+ while high - low < 2 :
10
+ mid = (low + high ) // 2
11
+ if li [mid ] < v :
12
+ high = mid
13
+ else :
14
+ low = mid
15
+
16
+ return low
17
+
18
+ def main ():
19
+ N , K = map (int , input ().split ())
20
+ gems = []
21
+ packs = []
22
+
23
+ for _ in range (N ):
24
+ M , V = map (int , input ().split ())
25
+ gems .append ((V , M ))
26
+
27
+ for _ in range (K ):
28
+ packs .append (int (input ()))
29
+
30
+ gems .sort (reverse = True )
31
+ packs .sort ()
32
+
33
+ res = 0
34
+
35
+ for V , M in gems :
36
+ pack_idx = lowerbound (packs , M )
37
+ if not pack_idx == None :
38
+ packs .pop (pack_idx )
39
+ res += V
40
+ if len (packs ) == 0 :
41
+ break
42
+
43
+ print (res )
44
+
45
+ main ()
Original file line number Diff line number Diff line change
1
+ 3 2
2
+ 1 65
3
+ 5 23
4
+ 2 99
5
+ 10
6
+ 2
7
+
8
+ 164
You can’t perform that action at this time.
0 commit comments