-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHOCOLA.java
69 lines (56 loc) · 1.22 KB
/
CHOCOLA.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Problem link - https://www.spoj.com/problems/CHOCOLA/
// TC - O((n log n) + (m log m))
// SC - O(1)
import java.util.*;
import java.lang.*;
import java.util.Arrays;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
int m = sc.nextInt() - 1;
int n = sc.nextInt() - 1;
int[] arr1 = new int[m];
int[] arr2 = new int[n];
for(int i = 0; i < m; i++){
arr1[i] = sc.nextInt();
}
for(int i = 0; i < n; i++){
arr2[i] = sc.nextInt();
}
long ans = minCost(arr1, arr2, m, n);
System.out.println(ans);
}
}
private static int minCost(int[] x, int[] y, int m, int n){
Arrays.sort(x);
Arrays.sort(y);
int hCount = 1; //horizontal count
int vCount = 1; // vertical count
int ans = 0;
int i = m - 1, j = n - 1;
while(i >= 0 && j >= 0){
if(x[i] > y[j]){
ans += x[i] * vCount;
hCount++;
i--;
} else {
ans += y[j] * hCount;
vCount++;
j--;
}
}
while(i >= 0){
ans += x[i] * vCount;
i--;
}
while(j >= 0){
ans += y[j] * hCount;
j--;
}
return ans;
}
}