Mega Sale
Basic Accuracy: 39.04% Submissions: 502 Points: 1

Mr. Geek is a greedy seller. He has a stock of N laptops which comprises of both useful and useless laptops. Now, he wants to organize a sale to clear his stock of useless laptops. The prices of N laptops are Ai each consisting of positive and negative integers (-ve denoting useless laptops). In a day, he can sell atmost M laptops. Mr. Geek being a greedy seller want to earn maximum profit out of this sale. So, help him maximizing his profit by selling useless laptops.

 

Example 1:

Input:
N=4, M=3
A[] = {-6, 0, 35, 4}
Output:
6
Explanation:
Geek sells the laptops with price -6 and
earns Rs. 6 as profit.

 

Example 2:

Input:
N=5, M=10
A[] = {1, -2, -3, -4, 5}
Output:
9
Explanation:
Geek sells the laptops with price -2,
-3 and -4 and earns Rs. 9 as profit.

 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function maxProfit() which takes the array A[], its size N and an integer M as inputs and returns the maximum profit Mr. Geek can earn in a single day.


 

Expected time Complexity: O(N)
Expected time Complexity: O(1)

 

Constraints:
1 ≤ N ≤ 105
1 ≤ M ≤ 105
-106 ≤ Ai ≤ 106

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Mega Sale

Output Window