ad7312fe8b81a20644b823c631559d50c0cd74aba77c88147cba29f2e120c0e2
// https://szkopul.edu.pl/problemset/problem/0qINw7rrL7Bz0W90zn2L5BiV/site/?key=statement
// OIG X (3 etap)
#include <iostream>
#include <stack>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
int n;
std::cin >> n;
if (n == 1) {
std::cin >> n;
std::cout << n;
return 0;
}
std::stack<std::pair<int, int>> s;
int a;
std::cin >> a;
s.push({a, 1});
long long ans = a, local_sum = a;
for (int i = 1; i < n; i++) {
std::cin >> a;
int ile = 1;
while (!s.empty()) {
if (s.top().first <= a) {
ile += s.top().second;
local_sum -= (ll)s.top().second * s.top().first;
s.pop();
} else {
break;
}
}
s.push({a, ile});
local_sum += (ll)a * ile;
ans += local_sum;
}
std::cout << ans << '\n';
return 0;
}