Codeforces #402 (Div. 2) C. Dishonest Sellers

题目

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·105, 0 ≤ 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

Note

In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.

In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25.


题意:

n件物品,打折前价格为a,打折后价格为b。
给出物品件数n和打折前必须买的件数k。
怎么样买n件商品才能最划算。


思路:

用结构体数组存储每一个打折前和打折后的价格a、b在同一对象中。
将b-a存于b中,如果大于0,则商家打折还加价。则直接买打折前的物品。
统计完没有降价的物品后对结构体数组b进行排序。
统计已经买的打折前的物品数量是否大于等于k件物品
==》符合则直接把剩下的商品价格加上。
==》不符合则从b由大到小累加a统计至大于等于k件物品之后再加上剩下的物品的价格b+a


代码:

#include<stdio.h>
#include<algorithm>
using namespace std;

struct list{
    int a;
    int b;
};

list num[200005];

bool cmp(list m,list n)
{
    return m.b>n.b;
}

int main(){
    int n,k,i;
    scanf("%d%d",&n,&k);
    int key=0;
    int count=0;
    for(i=0;i<n;i++){
        scanf("%d",&num[i].a);
    }
    for(i=0;i<n;i++){
        scanf("%d",&num[i].b);
    }
    for(i=0;i<n;i++){
        num[i].b=num[i].b-num[i].a;
        if(num[i].b>=0){
            count+=num[i].a;
            key++;
        }
    }
    sort(num,num+n,cmp);
    if(key<k){
        for(i=0;i<n;i++){
            if(key==k)  break;
            if(num[i].b<0){
                count+=num[i].a;
                key++;  
            }
        }
    }
    for(i=key;i<n;i++){
        count+=num[i].b+num[i].a;
    }
    printf("%d\n",count);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值