Mega Sale

QUESTION

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.\n\nInput:\n\nFirst line of the input contains T denoting the number of test cases.Each test case has 2 lines :\n\nfirst line has two spaced integers n m.\nsecond line has n integers [a0…ai…an-1].\nOutput:\n\nThe maximum sum of money that LALU can earn, given that he can carry at most m Laptops.\n\n\nConstraints:\n\n1<=T<=10\n\n1<=n<=m<=100\n\n-1000<=ai<=1000.

“TESTCASE_1”: “1\n5 3\n-6 0 35 -2 4\n###—###SEPERATOR—###—\n8”, “TESTCASE_2”: “2\n6 2\n-4 5 0 -1 2 1\n7 4\n6 1 0 -2 2 -4 3\n###—###SEPERATOR—###—\n5\n6”, “TESTCASE_3”: “2\n6 2\n-1 0 -4 -6 4 3\n7 4\n-1 -6 -2 -3 6 1 0\n###—###SEPERATOR—###—\n10\n12”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<iostream>
#include<math.h>
#include <stdlib.h> 
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 ;
}       
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
CHP Adblock Detector Plugin | Codehelppro