zoukankan      html  css  js  c++  java
  • Codeforces Round #402 (Div. 2) C. Dishonest Sellers (贪心)

    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.

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef long long ll;
    
    const int maxn=2e5+10;
    
    pair<pair<int,int>,int> p[maxn];
    
    int cmp(pair<pair<int,int>,int> a,pair<pair<int,int>,int> b){
        if(a.second==b.second){
            if(a.first.first==b.first.first){
                return a.first.second<b.first.second;
            }
            return a.first.first<b.first.first;
        }
        return a.second<b.second;
    }
    
    int main(){
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,k;
        cin>>n>>k;
        for(int i=0;i<n;i++){
            cin>>p[i].first.first;
        }
        for(int i=0;i<n;i++){
            cin>>p[i].first.second;
            p[i].second=p[i].first.first-p[i].first.second;
        }
        sort(p,p+n,cmp);
        int num=1;
        ll ans=0;
        for(int i=0;i<n;i++){
            if(p[i].second<=0){
                ans+=p[i].first.first;
                num++;
            }
            if(p[i].second>0){
                if(num<=k) ans+=p[i].first.first;
                else ans+=p[i].first.second;
                num++;
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    
  • 相关阅读:
    C++ 对象模型学习记录(2) 第3章 data语义学
    C++ 对象模型学习记录(1) 第2章 构造函数语义学
    C ++ 对象模型学习记录(4) function 语义学 (未完待续)
    C++ 对象模型学习记录(3) 第1章 关于对象(未完)
    设计模式复习 之 代理模式
    大数运算
    effective C ++ 学习笔记之 item 31 将文件间的编译依赖关系降至最低(未完成)
    Java 复习 之1 多线程
    SQL中char varchar nchar nvarchar ntext区别和使用(资料汇总)
    .Net中的加密解密
  • 原文地址:https://www.cnblogs.com/hymscott/p/6445618.html
Copyright © 2011-2022 走看看