#P3971. [TJOI2014] Alice and Bob

[TJOI2014] Alice and Bob

Description

Alice and Bob invented a new game. Given a sequence {x0,x1,,xn1}\{x_0, x_1, \cdots, x_{n-1}\}, Alice gets a sequence {a0,a1,,an1}\{a_0, a_1, \cdots, a_{n-1}\}, where aia_i is the length of the longest increasing subsequence ending at xix_i; Bob gets a sequence {b0,b1,,bn1}\{b_0, b_1, \cdots, b_{n-1}\}, where bib_i is the length of the longest decreasing subsequence starting at xix_i. Alice’s score is the sum of the sequence {ai}\{a_i\}, and Bob’s score is the sum of the sequence {bi}\{b_i\}.

Input Format

The first line contains nn, and the second line contains the sequence {a0,a1,,an1}\{a_0, a_1, \cdots, a_{n-1}\}. It is guaranteed that the sequence {ai}\{a_i\} can be obtained from at least one permutation of 11 to nn.

Output Format

Output a single line indicating the highest score Bob can obtain given the sequence {ai}\{a_i\}.

4
1 2 2 3
5
4
1 1 2 3
5

Hint

Constraints

For 30%30\% of the testdata, n1000n \le 1000.

For 100%100\% of the testdata, n105n \le 10^5.

Translated by ChatGPT 5