forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12.cpp
46 lines (40 loc) Β· 1.15 KB
/
12.cpp
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';
}