forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12.java
More file actions
49 lines (41 loc) ยท 1.41 KB
/
12.java
File metadata and controls
49 lines (41 loc) ยท 1.41 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
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);
}
}