AmazonOA #384
Maximize Negative Signs
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsGreedy
Assign a plus or minus sign to each element of an array of positive integers so that the signed sum of every non-empty prefix is strictly greater than 0. Maximize the number of minus signs.
Input
The first line contains n, and the second line contains n integers. Original bounds: 1≤n≤100000, 1≤a[i]≤10^9; a prefix sum of 0 is invalid.
Output
Output a single integer answer.
Example 1
Input:
3
1 1 1
Output:
1
Example 2
Input:
4
5 1 1 1
Output:
3
Example 3
Input:
4
2 3 1 4
Output:
1
Example notes
The outputs of the three public examples are, in order: 1; 3; 1. Verified by independent enumeration or direct simulation.
Editorial
Loading solution…