#4985. Problem 1. Min Max Subarrays
Problem 1. Min Max Subarrays
Problem 1. Min Max Subarrays
USACO 2025 February Contest, Platinum
Note: The time limit for this problem is 3s, 1.5x the default.
You are given a length- integer array (). Output the sum of the answers for the subproblem below over all contiguous subarrays of .
Given a nonempty list of integers, alternate the following operations (starting with the first operation) until the list has size exactly one.
- Replace two consecutive integers in the list with their minimum.
- Replace two consecutive integers in the list with their maximum.
Determine the maximum possible value of the final remaining integer.
For example,
[4, 10, 3] -> [4, 3] -> [4] [3, 4, 10] -> [3, 10] -> [10]
In the first array, is replaced by and is replaced by .
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains .
The second line contains .
OUTPUT FORMAT (print output to the terminal / stdout):
The sum of the answer to the subproblem over all subarrays.
SAMPLE INPUT:
2 2 1
SAMPLE OUTPUT:
4
The answer for is , the answer for is , and the answer for is .
Thus, our output should be .
SAMPLE INPUT:
3 3 1 3
SAMPLE OUTPUT:
12
SAMPLE INPUT:
4 2 4 1 3
SAMPLE OUTPUT:
22
Consider the subarray .
- Applying the first operation on (1, 3), our new array is .
- Applying the second operation on (4, 1), our new array is .
- Applying the third operation on (2, 4), our final number is .
It can be proven that is the maximum possible value of the final number.
SCORING: Inputs 4-5: Inputs 6-7: Inputs 8-9: Inputs 10-13: No additional constraints.
Problem credits: Benjamin Qi