Hust oj 1369 Buy Computers(水题)

Buy Computers
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 150(78 users)Total Accepted: 79(75 users)Rating: Special Judge: No
Description

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 ≤ mn ≤ 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的最大负数和是多少。。大水题

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 105;
int sum;
int n,m;
int t;
int a[maxn];

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sum = 0;
        int num = 0;
        sort(a,a+n);
        for(int i=0;i<n;i++)
        {
            if(a[i] < 0 && num < m)
            {
                num++;
                sum += a[i];
            }
        }
        printf("%d\n",-sum);
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值