C. Dishonest Sellers

来源:互联网 发布:手机录像播放软件 编辑:程序博客网 时间:2024/06/04 18:18

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 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.


这道题我处理的,分析的很冷静,关键点我觉得在于我用的struct 结构体,并且排序做的很好。


#include<stdio.h>#include<string.h>#include<stdlib.h>int a[200005],b[200005];struct node {int start;int end;int up;};struct node q[200005];int comp(const void *a,const void *b){return ((struct node *)b)->up - ((struct node *)a)->up;}int n,k;int main(){int i,j,value;scanf("%d %d",&n,&k);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)scanf("%d",&b[i]);for(i=0;i<n;i++){q[i].start=a[i];q[i].end=b[i];q[i].up=b[i]-a[i];}qsort(q,n,sizeof(q[0]),comp);for(i=0;i<n;i++)//printf("up=%d\n",q[i].up);value=0;int flag=0;for(i=0;i<k;i++)value+=q[i].start;//printf("value=%d\n",value);flag=k-1;for(i=k;i<n;i++){if(q[i].up>=0){flag=i;value+=q[i].start;}}for(i=flag+1;i<n;i++)value+=q[i].end;printf("%d\n",value);return 0;}


0 0