MicrosoftOA #65
Maximum Sum with Tiles
Estimated MediumJudge-ready
Topics (AI-labelled)State Machines
Place at most three tiles, each covering two adjacent elements of the original array. Tiles cannot overlap or go out of bounds, and you may place none at all. Find the maximum total of the covered elements.
This site uses a standard input/output protocol.
Input
The first line contains N, and the second line contains N integers. This site uses 1≤N≤200000 and elements in −10⁹..10⁹.
Output
Output a single integer, the answer.
Example 1
Input:
9
2 3 5 2 3 4 6 4 1
Output:
25
Example 2
Input:
5
1 2 3 3 2
Output:
10
Example 3
Input:
2
-1 -2
Output:
0
Example notes
Example 1: cover 3+5, 3+4, 6+4, for a total of 25. Example 2: cover 2+3 and 3+2, for a total of 10, needing only two tiles. Example 3: all numbers are negative, so placing no tile gives 0.
Editorial
Loading solution…