hdu 3594 Buy Computers【水题】

来源:互联网 发布:廊坊用友软件 编辑:程序博客网 时间:2024/05/09 05:16

Buy ComputersTime Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 153(80 users)Total Accepted: 81(77 users)Rating: Special Judge: NoDescription

Leyni goes to a second-hand market for old computers. There are n computers at the sale and computer i costs ci Yuan. Some computers with a negative price mean that their owners will pay Leyni if he buys them. Leyni can carry at most m computers, and he won’t go to the market for a second time.

Leyni wonders the maximum sum of money that he can earn.

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.

For each test case:

Line 1. This line contains two space-separated integers n and m (1 ≤ m ≤ n ≤ 100) indicating the amount of computers at the sale and the amount of computers that Leyni can carry.

Line 2. This line contains n space-separated integers ci (-1000 ≤ ci ≤ 1000) indicating the prices of the computers.

Output

For each test case:

Line 1. Output the maximum sum of money that Leyni can earn.

Sample Input

1

5 3

-5 3 2 1 -4

Sample Output

9

Source哈理工2012春季校赛 - 网络预选赛Author齐达拉图@HRBUST

题目大意:就是初始的时候,身上最多可以带m个电脑去卖,负值表示这个店铺买我一个电脑的支付金额,问我这一趟最多能赚多少钱。


思路:

sort一下,累加最小值,输出负值即可。


Ac代码:


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int a[50000];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,m;        scanf("%d%d",&n,&m);        for(int i=0;i<n;i++)        {            scanf("%d",&a[i]);        }        sort(a,a+n);        int output=0;        int tt=m;        for(int i=0;i<n;i++)        {            if(tt==0)break;            if(a[i]<0)            {                output+=a[i];                tt--;            }        }        printf("%d\n",-output);    }}






0 0