F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Longest Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1145    Accepted Submission(s): 291


Problem Description
Bobo has a sequence $a_1, a_2, \dots, a_n$.
Let $f(x)$ be the length of longest *strictly* increasing subsequence after replacing all the occurrence of $0$ with $x$.
He would like to find $\sum_{i = 1}^n i \cdot f(i)$.

Note that the length of longest strictly increasing subsequence of sequence $s_1, s_2, \dots, s_m$ is the largest $k$
such that there exists $1 \leq i_1 < i_2 < \dots < i_k \leq m$ satisfying $s_{i_1} < s_{i_2} < \dots < s_{i_k}$.
 

Input
The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains an integer $n$.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$.
 

Output
For each test case, print an integer which denotes the result.

## Constraint

* $1 \leq n \leq 10^5$
* $0 \leq a_i \leq n$
* The sum of $n$ does not exceed $250,000$.
 

Sample Input
2 1 1 3 1 0 3 6 4 0 6 1 0 3
 

Sample Output
3 14 49
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-11-22 19:06:29, Gzip enabled