#P7421. 「PMOI-2」子序列
「PMOI-2」子序列
Background
When you see this time limit, there is no need to panic. This problem does not rely on constant-factor tricks.
Problem Description
You are given a sequence of length (indices start from ). You need to choose a non-empty subsequence of with any length. Let the length of be . Define . You need to maximize:
$$\sum_{i=0}^{l}(a_{p(i+1)}-a_{p(i)})(p(l-i+1)-p(l-i))$$Output the maximum value.
If is a subsequence of , then can be obtained by deleting zero or more elements from while keeping the original order. For example, is a subsequence of , is a subsequence of , and is not a subsequence of .
Input Format
The first line contains a positive integer , representing the length of .
The second line contains integers, where the -th number is .
Output Format
Output one integer, representing the maximum value of $\sum_{i=0}^{l}(a_{p(i+1)}-a_{p(i)})(p(l-i+1)-p(l-i))$.
3
1 5 2
2
5
-2 3 2 5 3
15
Hint
[Sample Explanation]
For the first sample, choosing gives the maximum result .
For the second sample, choosing gives the maximum result $(-2-0)\times(6-5)+[3-(-2)]\times(5-1)+(0-3)\times(1-0)=15$.
[Constraints]
For of the testdata, and .
For of the testdata, .
For of the testdata, and .
Translated by ChatGPT 5