CodeForces - 363D Renting Bikes

Renting Bikes
Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

Description

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 ≤ 1050 ≤ a ≤ 109). The second line contains the sequence of integersb1, 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 integersp1, 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 rentr bikes. If the schoolchildren cannot rent any bikes, then r = s = 0.

Sample Input

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


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
bool cmp(int a,int b){return a>b;}
const int maxn=2e5+87;
int n,m,a;
ll b[maxn],p[maxn];    ///money,price
ll ans=0;

bool ok(int k)
{
    ll t=a;
    ans=0;
    for(int i=k-1,j=0;i>=0&&t>=0;i--,j++)
    {
        if(b[i]>=p[j])
        {
            ///ans+=p[j];
        }
        else{
           /// ans+=b[i];
            t-=p[j]-b[i];
            if(t<0)return 0;
        }
    }
    return t>=0;
}
int main()
{
    cin>>n>>m>>a;
    for(int i=0;i<n;i++)cin>>b[i];
    for(int i=0;i<m;i++)cin>>p[i];
    sort(b,b+n,cmp);
    sort(p,p+m);
  ///  for(int i=0;i<m;i++)cout<<p[i]<<' ';    cout<<endl;
    int l=0,r=min(n,m);
    while(l<r)
    {
        int mid=(l+r+1)/2;
        ans=0;
        if(ok(mid))
            l=mid;
        else r=mid-1;
    }
    ans=0;
    for(int i=0;i<l;i++)ans+=p[i];
    ans-=a;
    if(ans<0)ans=0;
    cout<<l<<" "<<ans<<endl;
}


你可能感兴趣的:(Algorithm,dp,ACM,Data,codeforces)