#include <bits/stdc++.h>
using namespace std;
int t[888888];
int n;
vector<int> a;
void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = a[tl];
    return;
  }
  int tm = (tl + tr) / 2;
  build(v * 2, tl, tm);
  build(v * 2 + 1, tm + 1, tr);
  t[v] = min(t[v * 2], t[v * 2 + 1]);
}
int getMin(int v, int tl, int tr, int l, int r) {
  if (l > r) return INT_MAX;
  if (tl == l && tr == r) {
    return t[v];
  }
  int tm = (tl + tr) / 2;
  int ans1 = INT_MAX, ans2 = INT_MAX;
  if (l <= min(r, tm)) ans1 = getMin(v * 2, tl, tm, l, min(r, tm));
  if (max(l, tm + 1) <= r)
    ans2 = getMin(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
  return min(ans1, ans2);
}
int find_min_pos(int x, int pos) {
  int l = pos, r = n + 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    int mn = getMin(1, 1, n, pos, m);
    if (mn >= x) {
      l = m;
    } else {
      r = m;
    }
  }
  return l + 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  a.resize(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  build(1, 1, n);
  vector<long long> ans(n + 2);
  set<pair<int, int>> numbers;
  ans[n] = a[n];
  for (int i = n - 1; i > 0; --i) {
    int pos = n;
    pos = find_min_pos(a[i], i);
    ans[i] = a[i] * (pos - i) + ans[pos];
  }
  long long answer = 0;
  for (int i = 1; i <= n; ++i) {
    answer += ans[i];
  }
  cout << answer;
  return 0;
}
