Dishonest Sellers

题目链接  http://codeforces.com/problemset/problem/779/C

C. Dishonest Sellers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.

Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.

Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.

Input

In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·1050 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.

The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).

The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).

Output

Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.

Examples
input
3 1
5 4 6
3 1 5
output
10
input
5 3
3 4 7 10 3
4 5 5 12 5
output
25
题目大意:

一个商店想要打折卖东西(但是店员并不是很诚实,有些商品打完折后更贵)。商店共有n件商品,一个人想要全部买了。这个人分两次买,第一次是在打折期买至少k件商品;第二次是在打折后买剩下的商品。问:这个人最少花费多少钱能够把n件商品买了。

按照第一组测试数据说吧,这样会更清晰。3 1 分别表示所有商品件数  该人第一次至少买的数目。第二行表示打折期n件商品的价格。第三行表示原价n件商品的价格。

解题思路:定义一个c[]数组表示c[i]=a[i]-b[i],然后对c[]数组进行从小到大排序。用sum记录按照原价所有商品的价格总和。然后让数组c的钱k项依次与sum相加。完后如果接下来的c[i]<0的话,那么继续相加(因为第一次至少买k件商品,如果多于k件并且打折后便宜,那么久继续买)

代码:

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n,k,ans,z;
    int a[200005],b[200005],c[200005];
    while(cin>>n>>k)
    {
        ans=0;
        for(int i=0;i<=n-1;i++)
            cin>>a[i];
        for(int i=0;i<=n-1;i++)
        {
            cin>>b[i];
            ans=ans+b[i];
        }
        for(int i=0;i<=n-1;i++)
            c[i]=a[i]-b[i];
        sort(c,c+n);
        for(int i=0;;i++)
        {
            if(k==0)          //如果k件商品买够了
            {
                if(c[i]<0)    //如果还打折期便宜的话就继续买
                {
                    ans=ans+c[i];
                    continue;
                }
                else         //否则表示打折后比打折前更贵,呢么就不买了,等打折期过了再买
                    break;
            }
            if(k>0)          //若果k件商品还没有买够,那么久继续买
            {
                ans=ans+c[i];
                k--;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值