Question Name:Mega Sale

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

    long long t;
    cin >> 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;
                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;
            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.


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].

The maximum sum of money that LALU can earn, given that he can carry at most m Laptops.



  • Test Case 1

    Input (stdin)

    5 3
    -6 0 35 -2 4

    Expected Output

  • Test Case 2

    Input (stdin)

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

    Expected Output

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.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock