| Buy Computers | ||||||
| ||||||
| 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 ≤ 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);
}
}
本文介绍了一道编程题,目标是在限定数量内选择最有利可图的旧电脑进行买卖以获得最大收益。通过排序和累加最低价格的方法解决此问题。

547

被折叠的 条评论
为什么被折叠?



