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 Java7 (OpenJDK 1.7.0) (judge verdict: Wrong Answer)

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

        Scanner cin=new Scanner(System.in);
        String a = cin.nextLine();
        String[] b = cin.nextLine().split(" ");

        int[] c = new int[Integer.parseInt(a)];

        for(int i = 0; i < c.length; i++){
        	c[i] = Integer.parseInt(b[i]);
        }

        int count = 0;
        int count1 = 0;

        int min = 0;

        long result = 0;
        for(int i = 0; i < c.length; i++){
        	min = c[i];
        	count = 1;
            for(int k = i + 1; k < c.length; k++){
            	if(min > c[k]){
            		count = count + k - i - 1;
            		break;
            	}else if( k == c.length - 1){
            		count = count + k - i;
            	}
            }
            count1 = 1;
            for(int k = i - 1; k > -1; k--){
            	if(min > c[k]){
            		count1 = count1 + i - 1 - k;
            		break;
            	}else if( k == 0){
            		count1 = count1 + i - k;
            	}
            }
            result = result + min * count * count1;
        }


        System.out.println(result);
	}

}