-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_118galgo7.java
More file actions
35 lines (35 loc) · 955 Bytes
/
_118galgo7.java
File metadata and controls
35 lines (35 loc) · 955 Bytes
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
import java.util.*;
public class _118galgo7 {
public static void main(String[] args) {
int n =4 , m = 6;
Integer costVer[]={2,1,3,1,4};
Integer costHor[]={4,1,2};
Arrays.sort(costVer,Collections.reverseOrder());
Arrays.sort(costHor,Collections.reverseOrder());
int h = 0, v =0;
int hp = 1, vp = 1;
int cost=0;
while(h<costHor.length && v<costVer.length){
if(costVer[v]<=costHor[h]){
cost+= (costHor[h]*vp);
hp++;
h++;
} else{
cost+= (costVer[v]*hp);
vp++;
v++;
}
}
while(h<costHor.length){
cost+= (costHor[h]*vp);
hp++;
h++;
}
while(v<costVer.length){
cost+= (costVer[v]*hp);
vp++;
v++;
}
System.out.println(cost);
}
}