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


TEST CASE 1 

INPUT
1
5 3
-6 0 35 -2 4
OUTPUT
8

TEST CASE 2 

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

#include<iostream>
using namespace std;
int MEGA_SALE(int [],int ,int ) ;
void bubble_sort(int [],int ) ;
int minof(int ,int ) ;
int main()
 {
 int t,arr[100],no,i,k ;
 cin>>t ;
 while(t--)
 {
     cin>>no ;
     cin>>k ;
     for(i=0;i<no;i++)
         cin>>arr[i] ;
        
     no=MEGA_SALE(arr,no,k) ;
     cout<<abs(no)<<endl ;
 }
 return 0;
}

int MEGA_SALE(int arr[],int no,int k)
{
    int i ;
    bubble_sort(arr,no) ;
   
    int sum=0 ;
    for(i=0;i<k;i++)
        sum=minof(sum,sum+arr[i]) ;
       
    return sum ;
}

void bubble_sort(int arr[],int no)
{
    int i,j,temp ;
    for(i=0;i<no-1;i++)
    {
        for(j=0;j<no-i-1;j++)
        {
            if(arr[j]>arr[j+1])
            {
                temp=arr[j] ;
                arr[j]=arr[j+1] ;
                arr[j+1]=temp ;
            }
        }
    }
}

int minof(int a,int b)
{
    return a>b?b:a ;
}       

Problem Description

LALU wants to maximize his earnings from a sale where he can buy a maximum of m laptops. Some laptops have negative prices, meaning the sellers are willing to pay him if he buys those laptops. Given the constraints of the sale, you need to help LALU determine the maximum amount of money he can earn.

Solution Approach

To solve the problem, the main goal is to select up to m laptops with the most negative prices (since they will pay LALU) to maximize his total earnings. Here's a step-by-step breakdown of the provided solution:


#include <iostream> using namespace std; // Function to calculate the maximum sum LALU can earn int MEGA_SALE(int arr[], int no, int k); // Function to sort the array using bubble sort void bubble_sort(int arr[], int no); // Function to find the minimum of two integers int minof(int a, int b); int main() { int t, arr[100], no, i, k; cin >> t; // Number of test cases while (t--) { cin >> no; // Number of laptops cin >> k; // Maximum number of laptops LALU can carry for (i = 0; i < no; i++) cin >> arr[i]; // Reading the cost of each laptop // Compute the maximum money LALU can earn no = MEGA_SALE(arr, no, k); cout << abs(no) << endl; // Print the result } return 0; } // Function to compute the maximum earnings int MEGA_SALE(int arr[], int no, int k) { int i; bubble_sort(arr, no); // Sort the array in ascending order int sum = 0; // Sum up the k smallest (most negative) values to maximize the earnings for (i = 0; i < k; i++) sum = minof(sum, sum + arr[i]); return sum; } // Bubble sort function to sort the array void bubble_sort(int arr[], int no) { int i, j, temp; for (i = 0; i < no - 1; i++) { for (j = 0; j < no - i - 1; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // Function to return the minimum of two integers int minof(int a, int b) { return a > b ? b : a; }



Detailed Explanation

  1. Input Handling:

    • The program reads multiple test cases. For each test case, it reads the number of laptops (no), the maximum number of laptops LALU can carry (k), and the list of laptop prices.
  2. Sorting:

    • The bubble_sort function is used to sort the laptop prices in ascending order. Sorting is necessary to identify the most negative prices easily.
  3. Calculating Maximum Earnings:

    • The MEGA_SALE function sums up the k smallest (most negative) prices from the sorted list. This is done because these are the prices where LALU can earn the most money.
    • The minof function ensures that the sum does not exceed the number of laptops LALU can carry.
  4. Output:

    • The result is printed after converting the sum to its absolute value, since the prices are negative and LALU earns money based on their absolute values.