Showing:
Handle Score
@Ibrahim Nash 6560
@mb1973 5777
@Quandray 5271
@akhayrutdinov 5111
@saiujwal13083 5074
@kirtidee18 4356
@sanjay05 3762
@mantu_singh 3638
@gfgaccount 3601
@marius_valentin_dragoi 3525
Mega Sale
Basic Accuracy: 39.56% Submissions: 369 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.

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