public static int chocola(int n, int m,int[] hArray, int [] vArray) {
Arrays.sort(hArray);
Arrays.sort(vArray);
int h=0,v=0,hp=1,vp=1;
int cost=0;
while(h<hArray.length && v<vArray.length){
if(vArray[vArray.length-v-1]<=hArray[hArray.length-h-1]){
cost+=hArray[hArray.length-h-1]*vp;
hp++;
h++;
}
else{
cost+=vArray[vArray.length-v-1]*hp;
vp++;
v++;
}
}
while(h<hArray.length){
cost+=hArray[hArray.length-h-1]*vp;
hp++;
h++;
}
while(v<vArray.length){
cost+=vArray[vArray.length-v-1]*hp;
vp++;
v++;
}
return cost;
}
public static void main(String[] args){
String s= new String("hi");
System.out.println(s.);
int n=4, m=6;
int[] hArray={2,1,3,1,4};
int[] vArray={4,1,2};
System.out.println(chocola(n,m,hArray,vArray));
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter