Codeforces Round #402 (Div. 2) C

Description

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 15 4 63 1 5

output

10

input

5 33 4 7 10 34 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.

题意:给我们当前的价格和一周后的价格,当前至少买k个物品,下周买n-k个物品,问总的花费最小

解法:以为是dp,后来用贪心过了,我们先比较两个价格的变化,优先买价格变化小的(只能买当前价格),接下来买价格中最便宜的。

我们记录价格变化,从小到大排序,然后前k个数字相加,然后买价格最便宜的

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <math.h>
 5 #include <iostream>   // C++头文件,C++完全兼容C
 6 #include <algorithm>  // C++头文件,C++完全兼容C
 7 #define fre  freopen("in.txt","r",stdin)   //以文件代替控制台输入,比赛时很常用,能缩短输入测试样例的时间
 8 #define INF 0x3f3f3f3f
 9 #define inf 1e60
10 using namespace std;  // C++头文件,C++完全兼容C
11 #define N 200005       // 宏定义
12 #define LL long long  //宏定义
13
14 int a[N],b[N];
15 struct P
16 {
17     int x,y,ans;
18 }H[N];
19 int ans;
20 bool cmd(P a,P b)
21 {
22     return a.ans<b.ans;
23 }
24
25 int main()
26 {
27     int n,k;
28     cin>>n>>k;
29     for(int i=1;i<=n;i++)
30     {
31         cin>>H[i].x;
32     }
33     for(int i=1;i<=n;i++)
34     {
35         cin>>H[i].y;
36         H[i].ans=H[i].x-H[i].y;
37     }
38     sort(H+1,H+1+n,cmd);
39     for(int i=1;i<=k;i++)
40     {
41         ans+=H[i].x;
42     }
43
44     for(int i=k+1;i<=n;i++)
45     {
46         ans+=min(H[i].x,H[i].y);
47     }
48     cout<<ans<<endl;
49     return 0;
50 }
时间: 2024-07-31 14:29:08

Codeforces Round #402 (Div. 2) C的相关文章

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

题目链接:Codeforces Round #402 (Div. 2) C. Dishonest Sellers 题意: 有n个商品,每个商品这一周为ai的价格,下一周为bi的价格. 现在那个人要将这n个商品全部买掉,这一周最少要买k个商品, 为最小的花费是多少. 题解: xjb贪心一下. 按ai-bi排序,为负的要全部买掉,然后如果小于k件,就买前k件. 最后再买下一周的. 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i&

Codeforces Round #402 (Div. 2) D. String Game

题目链接:Codeforces Round #402 (Div. 2) D. String Game 题意: 给你两个字符串a,b,然后给你n=strlen(a)个数字n1,n2,...,nn,表示依次删a[ni-1]个字符. 当a串删到有k(k任意)个子串组合起来(顺序不变)刚好等于b串时,就不能删了. 比如 abba->(ab a) 刚好包括 aba ,bba不包括aab. 问最多能删多少次. 题解: 最开始还以为是要用某种数据结构啥的,结果都想复杂了,直接二分答案就行. 然后线性判断删掉后

Codeforces Round #402 (Div. 2) B

Description Polycarp is crazy about round numbers. He especially likes the numbers divisible by 10k. In the given number of n Polycarp wants to remove the least number of digits to get a number that is divisible by 10k. For example, if k?=?3, in the

【DFS】Codeforces Round #402 (Div. 2) B. Weird Rounding

暴搜 #include<cstdio> #include<algorithm> using namespace std; int n,K,Div=1,a[21],m,ans=100; bool vis[21]; void calc(int now) { int t=0; bool flag=0; for(int i=m;i>=1;--i) if(!vis[i]) { if((!flag) && a[i]==0) return; t=t*10+a[i]; fla

Codeforces Round #402 (Div. 2)

补的,不过都是自己做的. A.Pupils Redistribution [数学] 题意:交换A.B两数组中的元素,使得两组数组含1.2.3.4.5元素的个数相等. 做法:统计A组中1~5的个数,B组中减去.统计正数/2.负数/2绝对值,求两者最大值.数学问题,自己推一下. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #include <bits/stdc++.h> #defi

Codeforces Round #402 (Div. 2) A

Description In Berland each high school student is characterized by academic performance - integer value between 1 and 5. In high school 0xFF there are two groups of pupils: the group A and the group B. Each group consists of exactly n students. An a

Codeforces Round #402 (Div. 2) D

Description Little Nastya has a hobby, she likes to remove some letters from word, to obtain another word. But it turns out to be pretty hard for her, because she is too young. Therefore, her brother Sergey always helps her. Sergey gives Nastya the w

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i