forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path12.java
49 lines (41 loc) Β· 1.41 KB
/
12.java
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
47
48
49
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Nκ³Ό Kλ₯Ό μ
λ ₯λ°κΈ°
int n = sc.nextInt();
int k = sc.nextInt();
// λ°°μ΄ Aμ λͺ¨λ μμλ₯Ό μ
λ ₯λ°κΈ°
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
// λ°°μ΄ Bμ λͺ¨λ μμλ₯Ό μ
λ ₯λ°κΈ°
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++) {
b[i] = sc.nextInt();
}
// λ°°μ΄ Aλ μ€λ¦μ°¨μ μ λ ¬ μν
Arrays.sort(a);
// λ°°μ΄ Bλ λ΄λ¦Όμ°¨μ μ λ ¬ μν
Arrays.sort(b, Collections.reverseOrder());
// 첫 λ²μ§Έ μΈλ±μ€λΆν° νμΈνλ©°, λ λ°°μ΄μ μμλ₯Ό μ΅λ Kλ² λΉκ΅
for (int i = 0; i < k; i++) {
// Aμ μμκ° Bμ μμλ³΄λ€ μμ κ²½μ°
if (a[i] < b[i]) {
// λ μμλ₯Ό κ΅μ²΄
int temp = a[i];
a[i] = b[i];
b[i] = temp;
}
// Aμ μμκ° Bμ μμλ³΄λ€ ν¬κ±°λ κ°μ λ, λ°λ³΅λ¬Έμ νμΆ
else break;
}
// λ°°μ΄ Aμ λͺ¨λ μμμ ν©μ μΆλ ₯
long result = 0;
for (int i = 0; i < n; i++) {
result += a[i];
}
System.out.println(result);
}
}