-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHOCOLA.java
58 lines (56 loc) · 1.13 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
package ques;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
public class CHOCOLA {
public static void main(String[] args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t>0){
t--;
br.readLine();
String[] s=br.readLine().split(" ");
int m=Integer.parseInt(s[0]);
int n=Integer.parseInt(s[1]);
int[] mw=new int[m-1];
int[] nw=new int[n-1];
for(int i=0;i<m-1;i++){
mw[i]=Integer.parseInt(br.readLine());
}
for(int i=0;i<n-1;i++){
nw[i]=Integer.parseInt(br.readLine());
}
int countm=1;
int countn=1;
Arrays.sort(mw);
Arrays.sort(nw);
int i=mw.length-1;
int j=nw.length-1;
int count=0;
int cost=0;
while(i>=0 && j>=0 && count<m*n){
if(mw[i]>nw[j]){
cost+=mw[i]*countn;
countm++;
i--;
}
else{
cost+=nw[j]*countm;
countn++;
j--;
}
}
while(i>=0){
cost+=mw[i]*countn;
countm++;
i--;
}
while(j>=0){
cost+=nw[j]*countm;
countn++;
j--;
}
System.out.println(cost);
}
}
}