Question Name:Mega Sale

#include <iostream>
#include <algorithm>
#include <cstring> 
using namespace std;
long long a[10000000];
int main() {

    long long t;
    cin >> t;

    while(t--){

        long long n;
        cin >> n;

        long long m;
        cin >> m;
        long long f =0;
        for(long long i =0;i<n;i++){
            long long k;
            cin >> k;
            if(k<0){
                a[f++] = k;
            }
        }
        sort(a, a+f);
        long long sum = 0;

        for(long long i =0;i<m;i++){
            sum += a[i];
        }
        if(f != 0){
            cout << sum*(-1) << endl;
        }
        else{
            cout << "0"  << endl;
        }
        memset(a, 0, n);
    }
return 0;
}

Problem 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

  • Test Case 1

    Input (stdin)

    1
    5 3
    -6 0 35 -2 4
    

    Expected Output

    8
  • Test Case 2

    Input (stdin)

    2
    6 2
    -4 5 0 -1 2 1
    7 4
    6 1 0 -2 2 -4 3
    

    Expected Output

    5
    6

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.