SORT10
QUESTION DESCRIPTION
LALU wanted to purchase a laptop so he went to a nearby sale.There were n Laptops at a sale.
Laptop with index i costs ai rupees.
Some Laptops have a negative price — their owners are ready to pay LALU if he buys their useless Laptop.
LALU can buy any Laptop he wants. Though he's very strong, he can carry at most m Laptops, and he has no desire to go to the sale for the second time.
Please, help LALU find out the maximum sum of money that he can earn.
Input:
First line of the input contains T denoting the number of test cases.Each test case has 2 lines :
First line has two spaced integers n m.
second line has n integers [a0...ai...an-1].
Output:
The maximum sum of money that LALU can earn, given that he can carry at most m Laptops.
Constraints:
1≤T≤10
1≤n,m≤100
-1000≤ai≤1000
LALU wanted to purchase a laptop so he went to a nearby sale.There were n Laptops at a sale.
Laptop with index i costs ai rupees.
Some Laptops have a negative price — their owners are ready to pay LALU if he buys their useless Laptop.
LALU can buy any Laptop he wants. Though he's very strong, he can carry at most m Laptops, and he has no desire to go to the sale for the second time.
Please, help LALU find out the maximum sum of money that he can earn.
Input:
First line of the input contains T denoting the number of test cases.Each test case has 2 lines :
First line has two spaced integers n m.
second line has n integers [a0...ai...an-1].
Output:
The maximum sum of money that LALU can earn, given that he can carry at most m Laptops.
Constraints:
1≤T≤10
1≤n,m≤100
-1000≤ai≤1000
TEST CASE 2
INPUT
INPUT
2
6 2
-4 5 0 -1 2 1
7 4
6 1 0 -2 2 -4 3
OUTPUT5
6
#include <stdio.h>
#include<stdlib.h>
void sort( int a[ ], int n ) {
int temp,k,i;
for(k = 0; k< n-1; k++) {
for(i = 0; i < n-k-1; i++) {
if(a[ i ] > a[ i+1] ) {
temp = a[ i ];
a[ i ] = a[ i+1 ];
a[ i + 1] = temp;
}
}
}
}
int main() {
int t,n,i,m,b;
int sum=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
int arr[n];
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
sort(arr,n);
for(i=0;i<m;i++)
{
if(arr[i]<0)
{
sum+=arr[i];
}
}
b=abs(sum);
printf("%d\n",b);
sum=0;
b=0;
}
return 0;
}
Comments
Post a Comment