Problem:
Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdint.h>

int N, a[212345];
int tmp;
uint64_t ans;

int main(void) {
	scanf("%d", &N);
	for (int i = 0; i < N; ++i) { scanf("%d", &a[i]); }
	
	for (int i = 0; i < N; ++i) {
		if (i == 0) { ans += (1 + N)*N / 2; }
		else {
			for (int j = 0; j < N - i; ++j) {
				if (j == 0) { tmp = a[j]; }
				else if (tmp > a[j]) { tmp = a[j]; }
			}
			ans += tmp;
		}
	}

	// TLE
	/*for (int l = 0; l < N; ++l) {
		for (int r = l; r < N; ++r) {
			for (int i = l; i <= r; ++i) {
				if (i == l) { tmp = a[i]; }
				if (tmp > a[i]) { tmp = a[i]; }
			}
			ans += tmp;
		}
	}*/

	printf("%I64d", ans);

	return 0;
}