AmazonOA #348
Sum Max Plus Min After Decrement Operations
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsSimulation
Perform exactly requests operations: first add the sum of the current maximum and the current minimum to the answer, then decrease one of the current maximums by 1. Find the exact final integer total. Both the original array and the values after operations may be negative; when several maximums tie, choosing any of them does not change the resulting multiset.
Input
The first line contains n requests and the second line contains n integers. The source only gives requests≥0, an integer array, and the use of sufficiently wide integers; on this site 1≤n≤200000, initial values are in −10^9..10^9, and 0≤requests≤10^12. The answer is not taken modulo anything and may exceed the signed 64-bit range.
Output
Output a single integer, the answer.
Example 1
Input:
2 2
1 2
Output:
5
Example 2
Input:
2 5
-1 -1
Output:
-20
Example 3
Input:
3 1
3 3 3
Output:
6
Example notes
The outputs of the three public examples are 5; -20; and 6, checked by independent enumeration or direct simulation.
Editorial
Loading solution…