QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 512 MB 満点: 100

#8634. Summation

統計

Given a sequence $\{a_i\}$ containing each number from $\{0, \dots, n-1\}$ exactly once, calculate:

$$ \sum_{l=1}^n\sum_{r=l}^n mex(\{a_l, \dots, a_r\}) $$

where $mex(S) = \min\{i \in \mathbb{N} \mid i \notin S\}$. Note: In this problem, $0 \in \mathbb{N}$.

Input

The input is read from standard input.

The input consists of two lines.

The first line contains a positive integer $n$.

The second line contains $n$ space-separated non-negative integers. It is guaranteed that these $n$ numbers are a permutation of $\{0, \dots, n-1\}$.

Output

Output to standard output.

Output a single integer representing the answer to the formula described in the problem.

Examples

Input 1

5
4 3 1 2 0

Output 1

14

Input 2

10
7 2 6 5 3 9 8 4 0 1

Output 2

40

Constraints

For $10\%$ of the data, $n \le 100$.

For $30\%$ of the data, $n \le 300$.

For $50\%$ of the data, $n \le 5000$.

For all test cases, $1 \le n \le 10^6$, and the $n$ input numbers are a permutation of $\{0, \dots, n-1\}$.

Note

Please pay attention to the range of the answer!

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.