【Codeforces-402C】-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·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
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 <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL __int64
struct node
{
	int a,b,c;
	bool d;
}s[200010];
bool cmp(node a,node b)
{
	return a.c<b.c;
}
int main()
{
	int n,k,ant=0;
	LL ans=0;
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
		scanf("%d",&s[i].a);
	for(int i=1;i<=n;i++)
		scanf("%d",&s[i].b);
	for(int i=1;i<=n;i++)
	{
		if(s[i].a<=s[i].b)
		{
			ans+=s[i].a;
			ant++;
			s[i].d=true;		//記錄是否買過 
			s[i].c=INF;			//買過就不會再買,差值賦值爲INF 
		}	
		else
			s[i].c=s[i].a-s[i].b;
	}
	if(ant>=k)
	{
		for(int i=1;i<=n;i++)
			if(s[i].d==false)
				ans+=s[i].b;
	}	
	else
	{
		sort(s+1,s+1+n,cmp);
		k=k-ant;
		for(int i=1;i<=k;i++)
			ans+=s[i].a;
		for(int i=k+1;i<=n;i++)
			if(s[i].d==false)
				ans+=s[i].b;
	}
	printf("%I64d\n",ans);
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章