QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100

#15247. Alice and Bob

Statistics

Alice and Bob have invented a new game. Given a sequence $\{x_0, x_1, \dots, x_{n-1}\}$, Alice obtains a sequence $\{a_0, a_1, \dots, a_{n-1}\}$, where $a_i$ is the length of the longest increasing subsequence ending at $x_i$. Bob obtains a sequence $\{b_0, b_1, \dots, b_{n-1}\}$, where $b_i$ is the length of the longest decreasing subsequence starting at $x_i$. Alice's score is the sum of the sequence $\{a_0, a_1, \dots, a_{n-1}\}$, and Bob's score is the sum of the sequence $\{b_0, b_1, \dots, b_{n-1}\}$.

Input

The first line contains $n$, and the second line contains the sequence $\{a_0, a_1, \dots, a_{n-1}\}$. It is guaranteed that the sequence $a$ can be obtained from at least one permutation of $1$ to $n$.

Output

Output a single line representing the maximum score Bob can obtain.

Data Range

For $30\%$ of the data, $n \le 1000$. For $100\%$ of the data, $n \le 10^5$.

Examples

Input 1

4
1 2 2 3

Output 1

5

Input 2

4
1 1 2 3

Output 2

5

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.