#1013
Unrated
DIFERENCIJA
시간 제한
1s
메모리 제한
64MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Author: Adrian Satja Kurdija

Mirko discovered what Slavko did in previous task, and decided to deal with something completely opposite to tables of letters: sequences of numbers.

Let’s define a value of a sequence as the difference between the largest and the smallest number within that sequence. For example, value of sequence (3, 1, 7, 2) is 6, and value of (42, 42) is 0.

Find the sum of values of all subsequences of consecutive elements of a given sequence.

입력

The first line of input contains a single integer N (2 ≤ N ≤ 300 000), number of elements of the sequence.

Next N lines contain elements of the sequence. Each element is a positive integer not greater than 100 000 000.

출력

The first and only line of output must contain the requested sum.

예제 입력 1

3
1
2
3

예제 출력 1

4

예제 입력 2

4
7
5
7
5

예제 출력 2

12

예제 입력 3

4
3
1
7
2

예제 출력 3

31
코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.