Sdutoj greedy-businessman Xiao Xin

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.