PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000072. Những con số trong vụ án (Its a Murder!)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Những con số trong vụ án (Its a Murder!)

Its a Murder!

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Detective Saikat walks up a staircase to reach a crime scene. Each step bears an integer, and he reads the numbers in the order he encounters them.

When he reads value aia_i on step ii, he writes in his notebook the sum of the values on all earlier steps that are strictly smaller than aia_i. If there are no such values, he writes 00. Earlier values equal to aia_i are not included.

Finally, he adds all the recorded amounts. Compute this total. The sum concerns the values, not merely the number of qualifying earlier steps.

Input

The first line contains the number of steps nn.

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in traversal order.

Output

Print one integer: the sum of all amounts written in Saikat’s notebook.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; 0≤ai≤1060\le a_i\le 10^6 for every 1≤i≤n1\le i\le n.

  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 0≤ai≤1060\le a_i\le 10^6 for every 1≤i≤n1\le i\le n.

  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 0≤ai≤1060\le a_i\le 10^6 for every 1≤i≤n1\le i\le n.

Examples

Input

5
1 5 3 6 4

Output

15

Explanation

For [1,5,3,6,4][1,5,3,6,4], the five notebook entries are:

  • On 11, nothing is earlier: 00.
  • On 55, only 11 is smaller: 11.
  • On 33, only 11 is smaller: 11.
  • On 66, values 1,5,31,5,3 are smaller: 1+5+3=91+5+3=9.
  • On 44, values 1,31,3 are smaller: 1+3=41+3=4.

Their sum is 0+1+1+9+4=150+1+1+9+4=15, so print 15.