import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

void main(){
	int n = readln.chomp.to!int;
	int[] as = readln.chomp.split.map!(to!int).array;
	
	int[] life;
	life.length = n + 1;
	
	long ans;
	long sum = 0;
	int x = as[0];
	int len = 0;
	bool lenflag = 0;
	debug write("0: ");
	foreach(r; 0 .. n){
		if(as[r] < x){
			x = as[r];
			life[x] = 1;
			}
		else{
			life[x] += 1;
			}
		debug write(x, " ");
		sum += x;
		}
	
	ans += sum;
	debug writeln("=> ", sum, "; total=", ans);
	debug life.writeln;
	
	debug int loopcount = 0;
	
	foreach(l; 1 .. n){
		debug write(l, ": ");
		sum -= as[l - 1] * life[as[l - 1]];
		int rmax = l - 1 + life[as[l - 1]];
		x = n + 100;
		foreach(r; l .. rmax){
			debug loopcount += 1;
			if(as[r] < x){
				x = as[r];
				life[x] = 1;
				}
			else{
				life[x] += 1;
				}
			debug write(x, " ");
			sum += x;
			}
		ans += sum;
		debug writeln("=> ", sum, "; total=", ans);
		debug life.writeln;
		}
	
	
	ans.writeln;
	debug loopcount.writeln;
	}