forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCuttingBoards.java
More file actions
62 lines (59 loc) · 1.51 KB
/
CuttingBoards.java
File metadata and controls
62 lines (59 loc) · 1.51 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
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.*;
import java.io.*;
/*
* @author -- rajatgoyal715
*/
public class CuttingBoards {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
StringBuilder sb = new StringBuilder("");
while(t--!=0){
int m = sc.nextInt();
int n = sc.nextInt();
long y[] = new long[m-1];
for(int i=0;i<m-1;i++){
y[i] = sc.nextLong();
}
long x[] = new long[n-1];
for(int i=0;i<n-1;i++){
x[i] = sc.nextLong();
}
sb.append(cutBoard(m, n, y, x) + "\n");
}
System.out.println(sb.toString());
}
public static long cutBoard(int m, int n, long[] y, long[] x){
Arrays.sort(y);
Arrays.sort(x);
int horizontal = 1;
int vertical = 1;
int i=m-2;
int j=n-2;
long cost = 0;
long mod = 1000000007;
while(i>=0&&j>=0){
if(y[i]>=x[j]){
cost += vertical*y[i];
i--;
horizontal++;
}
else{
cost += horizontal*x[j];
j--;
vertical++;
}
}
while(j>=0){
cost += horizontal*x[j];
j--;
vertical++;
}
while(i>=0){
cost += vertical*y[i];
i--;
horizontal++;
}
return cost%mod;
}
}