CodeForces - 363D Renting Bikes (贪心+二分 最大值最小值)

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 nm 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.

题意:
    n个人的私房钱,bi表示第i个人的私房钱;m个车的价格,pi表示第i辆车的价格;还有一个a表示共有财产,n个人都能使用,自己的私房钱只能自己用;问总共最多租多少辆车并且花自己的私房钱最少??

思路: 对car的价格二分

     先对car和people sort一下

     想要租最多的car,肯定要买那mid个最便宜的car,为了钱能买到这么多的car,所以我们要用私房钱最多的那mid个people去买,然后判断缺的钱和共有财产的大小

代码:

   

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static int n,m;
	static long k;
	static final int max=100005;
	static int a[]=new int[max];
	static int b[]=new int[max];
	static int t=0;
	public static boolean check(int x){
		long sum=0;
		for(int i=x;i>=1;i--){
			 if(a[n+i-x]<b[i]){
				 sum+=(b[i]-a[n+i-x]);
			 }
		}
		if(sum<=k) return true;
		else return false;
	}
    public static void main(String[] args) {
		  Scanner scan=new Scanner(System.in);
		  n=scan.nextInt();
		  m=scan.nextInt();
		  k=scan.nextLong();
		  for(int i=1;i<=n;i++){
			  a[i]=scan.nextInt();
		  }
		  for(int i=1;i<=m;i++){
			  b[i]=scan.nextInt();
		  }
		  Arrays.sort(a,1,n+1);
		  Arrays.sort(b,1,m+1);
		  int l=1,r=Math.min(n, m),ans=0;
		  int mid=0;
		  while(l<=r){
			   mid=(l+r)/2;
			   if(check(mid)){
				     l=mid+1;
				     ans=mid;
			   }
			   else{
				    r=mid-1;
			   }
		  }
		  long sum=0;
		  for(int i=1;i<=ans;i++){
			  sum+=b[i];
		  }
		  int r1=ans;
		  long s=0;
		  if(sum<=k){
			   s=0;
		  }
		  else{
			  s=sum-k;
		  }
		  System.out.println(r1+" "+s);
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ksuper&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值