LinkedInOA #7
Optimize Context Switching
Estimated MediumJudge-ready
Topics (AI-labelled)DP BasicsPrefix & Ranges
You may only increase array elements, and each increment is an integer in 0..10^18. An interior point counts as a peak only if it is strictly greater than both neighbors; endpoints do not count. First maximize the number of peaks, then minimize the total increase.
Input
The first line contains n, and the second line contains n initial values. The original gives no bounds on n or the initial values; this site uses 1≤n≤200000 with initial values in −10^9..10^9, and keeps the original upper limit of 10^18 per increment.
Output
Output a single integer answer.
Example 1
Input:
3
1 1 1
Output:
1
Example 2
Input:
5
5 1 1 1 5
Output:
10
Example 3
Input:
2
1 2
Output:
0
Example notes
The outputs of the three public examples are, in order: 1; 10; 0. Checked by independent enumeration or direct simulation.
Editorial
Loading solution…