A group of n schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them.
The renting site offered them m bikes. The renting price is different for different bikes, renting the j-th bike costs pj rubles.
In total, the boys' shared budget is a rubles. Besides, each of them has his own personal money, the i-th boy has bi personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike.
Each boy can rent at most one bike, one cannot give his bike to somebody else.
What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible?
Input
The first line of the input contains three integers n, m and a (1 ≤ n, m ≤ 105; 0 ≤ a ≤ 109). The second line contains the sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104), where bi is the amount of the i-th boy's personal money. The third line contains the sequence of integers p1, p2, ..., pm (1 ≤ pj ≤ 109), where pj is the price for renting the j-th bike.
Output
Print two integers r and s, where r is the maximum number of schoolboys that can rent a bike and s is the minimum total personal money needed to rent r bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.
Examples
Input
2 2 10 5 5 7 6
Output
2 3
Input
4 5 2 8 1 1 2 6 3 7 5 2
Output
3 8
Note
In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money.
这种贪心运算我是真想不出来,太菜了,战斗圾加油!!!!!!!!!!!!!!!
核心思想,先算最多能买多少自行车,再在这种情况下,看最少能用多少钱
先看最多能买多少自行车,和田忌赛马类似,用最多的个人钱的人去买最少租金的车子
即,将存储每个人零花钱的数组,和存储每辆车子的租金的数组升序排列
将零花钱数组倒序和租金数组正序比较,这里我们就要用到二分法,减少比较次数,减少时间
即二分比较,
不够的钱可以拿公家的钱垫上
看代码
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static int n,m,a;
static int[] b=new int[100009];//存储零花钱
static int[] p=new int[100009];//存储租金
static boolean check(int x){//检测能不能买
long sum = 0;//long,int不行
for(int i=0;i<x;i++){
if(b[n-x+i]<p[i])倒序比正序,看这一段行不行,每个人都能买吗
sum+=p[i]-b[n-x+i];//差多少
}
return sum<=a;//差的钱是否能用公家钱垫上,能则这个范围可以
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
a=sc.nextInt();
for(int i=0;i<n;i++)
b[i]=sc.nextInt();
for(int i=0;i<m;i++)
p[i]=sc.nextInt();
Arrays.sort(b,0,n);
Arrays.sort(p,0,m);//升序
int left,right,mid,pre=-1;//pre记录范围边界,即能买到的自行车的最高租金位置,以便计算结果
left=0;
right=Math.min(n, m);//二分思想,这里要注意我们要取相对短的那一个,否则会超界
while(left<=right){
mid=(left+right)/2; //二分找中心
if(check(mid)==true){看这一半是否满足条件
left=mid+1;//满足向右找
pre=mid;//满足更新位置
}
else
right=mid-1;//不满足说明钱不够,向左找
}
System.out.print(pre+" ");
long s=0;
for(int i=0;i<pre;i++){
s+=p[i];
}
s=s-a;//统计租金总数-公家钱
System.out.println(s>0?s:0);//可以做到一分钱不花,全垫上
}
}