#include <bits/stdc++.h>
using namespace std;
const int k = 16;
const int N = 1e5;
const int ZERO = 1e9 + 1;
int table[N][k + 1];
int Arr[N];
int main() {
  int n, L, R, q;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> Arr[i];
  for (int i = 0; i < n; i++) table[i][0] = Arr[i];
  for (int j = 1; j <= k; j++) {
    for (int i = 0; i <= n - (1 << j); i++)
      table[i][j] = min(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    for (int p = i; p < n; p++) {
      if (i <= p) {
        L = i;
        R = p;
        int answer = ZERO;
        for (int j = k; j >= 0; j--) {
          if (L + (1 << j) - 1 <= R) {
            answer = min(answer, table[L][j]);
            L += 1 << j;
          }
        }
        ans += answer;
      }
    }
  }
  cout << ans;
  return 0;
}
