The topic describes Xiao Xin is a businessman, of course, businessmen most hope is more money, Xiao Xin is the same.
On this day, he came to a faraway country. There are n pieces of goods, for the first item I need to pay the price of CI to get. Of course, for item I, Xiao Xin has a valuation pi in his heart: it represents the price that can be sold when he buys the item and brings it back to his country. Xiao Xin can only bring back m pieces of merchandise, can you help him calculate how much he can make up money? Input input has multiple groups, to the end of the file. (Note: There are many groups of data, please use high-efficiency algorithms) for each set of data. The first line is n,m. m≤n≤10000000. followed by n rows, each row has two numbers C, p. Line I represents the Ci,pi. CI≤PI data is within the range of int. Output for each set of input data only output one line a number, which represents how much money Xiao Xin can make. Sample input
4 21 21 32 23 4
Sample output
3
#include <stdio.h> #include <string.h> #include <algorithm>using namespace std; #define N 10000001struct node{ int ci; int pi; int bi;} Ls[n];int CMP (node A,node b) { return a.bi>b.bi;} int main () { int n,m,i,j; while (~SCANF ("%d%d", &n,&m)) { int sum=0; for (i=0;i<=n-1;i++) { scanf ("%d%d", &ls[i].ci,&ls[i].pi); Ls[i].bi=ls[i].pi-ls[i].ci; } Sort (ls,ls+n,cmp); for (i=0;i<=m-1;i++) { sum=sum+ls[i].bi; } printf ("%d\n", sum); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sdutoj greedy-businessman Xiao Xin