商人小鑫
Problem Description
這天,他來到了一個遙遠的國度。那裏有着n件商品,對於第i件商品需要付出ci的價錢才能得到。當然,對於第i件商品,小鑫在自己心中有一個估價pi:代表着當他買下這件商品後帶回他的國家可以賣出的價格。小鑫只能帶回m件商品,你能幫他計算一下他最多能賺多少錢麼?
Input
Output
Sample Input
4 2 1 2 1 3 2 2 3 4
Sample Output
3
Hint
#include<stdio.h>
#include<stdlib.h>
#define Max 10000000
typedef struct node{
int ci;
int pi;
int money;
}thing;
thing th[Max+10];
int Compare(const void* a,const void* b){ //qsort()比較函數 倒序
thing* x=(thing*)a;
thing* y=(thing*)b;
return y->money - x->money; //小於0,a在前
}
int main(){
int n,m,sum;
while(~scanf("%d %d",&n,&m)){
sum=0;
for(int i=0;i<n;i++){
scanf("%d %d",&th[i].ci,&th[i].pi);
th[i].money=th[i].pi-th[i].ci;
}
qsort(th,n,sizeof(thing),Compare); //使用這個函數排序
for(int i=0;i<m;i++){
sum+=th[i].money;
}
printf("%d\n",sum);
}
return 0;
}