Hust oj 1369 Buy Computers(水题)

来源:互联网 发布:机器人授权系统源码 编辑:程序博客网 时间:2024/05/24 15:38
Buy ComputersTime Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 150(78 users)Total Accepted: 79(75 users)Rating: Special Judge: NoDescription

Leyni goes to a second-hand market for old computers. There aren 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 mostm 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 integerT indicating the number of test cases. Then T test cases follow.

For each test case:

Line 1. This line contains two space-separated integersn 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 integersci (-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;}


 

0 0
原创粉丝点击