forked from kothariji/competitive-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFAIRELCT.java
59 lines (56 loc) · 1.12 KB
/
FAIRELCT.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
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-- !=0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int a[]=new int[n];
int b[]=new int[m];
long sum1=0;
long sum2=0;
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
sum1+=a[i];
}
for(int i=0;i<m;i++)
{
b[i]=sc.nextInt();
sum2+=b[i];
}
Arrays.sort(a);
Arrays.sort(b);
int x=0;
int i=0;
int j=m-1;
while((i!=n)&&(j!=-1)&&(sum1<=sum2))
{
if(a[i]>=b[j])
{
break;
}
sum1=sum1-a[i]+b[j];
sum2=sum2-b[j]+a[i];
i++;
j--;
x++;
}
if(sum1>sum2)
{
System.out.println(x);
}
else{
System.out.println(-1);
}
}
}
}