forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12.cpp
More file actions
46 lines (40 loc) ยท 1.15 KB
/
12.cpp
File metadata and controls
46 lines (40 loc) ยท 1.15 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
#include <bits/stdc++.h>
using namespace std;
int n, k;
vector<int> a, b;
bool compare(int x, int y) {
return x > y;
}
int main(void) {
// N๊ณผ K๋ฅผ ์
๋ ฅ๋ฐ๊ธฐ
cin >> n >> k;
// ๋ฐฐ์ด A์ ๋ชจ๋ ์์๋ฅผ ์
๋ ฅ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.push_back(x);
}
// ๋ฐฐ์ด B์ ๋ชจ๋ ์์๋ฅผ ์
๋ ฅ๋ฐ๊ธฐ
for (int i = 0; i < n; i++) {
int x;
cin >> x;
b.push_back(x);
}
// ๋ฐฐ์ด A๋ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ ์ํ
sort(a.begin(), a.end());
// ๋ฐฐ์ด B๋ ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ ์ํ
sort(b.begin(), b.end(), compare);
// ์ฒซ ๋ฒ์งธ ์ธ๋ฑ์ค๋ถํฐ ํ์ธํ๋ฉฐ, ๋ ๋ฐฐ์ด์ ์์๋ฅผ ์ต๋ K๋ฒ ๋น๊ต
for (int i = 0; i < k; i++) {
// A์ ์์๊ฐ B์ ์์๋ณด๋ค ์์ ๊ฒฝ์ฐ
if (a[i] < b[i]) swap(a[i], b[i]); // ๋ ์์๋ฅผ ๊ต์ฒด
// A์ ์์๊ฐ B์ ์์๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์ ๋, ๋ฐ๋ณต๋ฌธ์ ํ์ถ
else break;
}
// ๋ฐฐ์ด A์ ๋ชจ๋ ์์์ ํฉ์ ์ถ๋ ฅ
long long result = 0;
for (int i = 0; i < n; i++) {
result += a[i];
}
cout << result << '\n';
}