Greedy Manager
Practice Questions
Greedy Manager
Problem Statement
You are a resource manager and you need to manage gold, you already have enough to fulfill everyone's need. There is a line of people who either give gold to you represented by positive value or take gold from you represented by a negative values.
You are greedy so you decided to get maximum profit so you decided to skip people but as you are under surveillance once you start accepting the person request you can only stop once and you need to take at least one person. You already have a list of people and their requests, so you can decide beforehand.
Boundary Condition(s)
1 <= t <= 10⁵
1 <= n <= 10⁵
-10⁴ <= a[i] <= 10⁴
Input Format
First line represents the number of test cases.
Second line represents the number of people.
Third line represents the input array.
Output Format
Number of extra gold, -ve if you give the gold.
Example Input/Output 1
Input
1
7
-2 -3 4 -1 2 -1 -1
Output
5
Explanation
You decided to skip the first two and then take 4 gold from the 3rd person, give 1 gold to the 4th person and again take 2 gold from the 5th person and then stop. In the end, you have 5 extra gold.
Comments