-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB_Helmets_in_Night_Light.java
87 lines (85 loc) · 2.22 KB
/
B_Helmets_in_Night_Light.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.Scanner;
public class B_Helmets_in_Night_Light{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
while(t>0){
int n,p;
n=in.nextInt();
p=in.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(int i=0;i<n;i++)
a[i]=in.nextInt();
for(int i=0;i<n;i++)
b[i]=in.nextInt();
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(b[j]>b[j+1])
{
int temp=a[j];
int temp1=b[j];
b[j]=b[j+1];
a[j]=a[j+1];
a[j+1]=temp;
b[j+1]=temp1;
}
}
}
int ans=0;
int c=n;
if(p>b[0]){
ans=p;
c=n-1;
for(int j=0;j<n;j++)
{
if(c==0)
break;
else
{
if(a[j]<=c)
{
ans=ans+(b[j]*a[j]);
c=c-a[j];
}
else
{
ans=ans+(c*b[j]);
c=0;
}
}
}}
else if(p==b[0])
{
int ans1=p*n;
ans=p;
c=n-1;
for(int j=0;j<n;j++)
{
if(c==0)
break;
else
{
if(a[j]<=c)
{
ans=ans+(b[j]*a[j]);
c=c-a[j];
}
else
{
ans=ans+(c*b[j]);
c=0;
}
}
}
ans=Math.min(ans,ans1);
}
else
{
ans=p*n;
}
System.out.println(ans);
t--;
}
}
}