    'use strict'
     
    process.stdin.resume();
    process.stdin.setEncoding('utf8');
     
    let line = ''
    process.stdin.on('data', function(chunk) {
      line = line + chunk
    });
     
    process.stdin.on('end', function() {
      const values = line.split(/\s+/)
      console.log(b(values.slice(1, values.length-1).map(x => parseInt(x))))
      process.exit(0)
    });
     
    function b(arr) {
  let total = 0
 let cache = new Array(arr.length)
 arr.reverse().forEach((num, index) => {
   let current = index - 1
   let cached = 0
   while (current !== -1) {
     if (arr[current] < num) {
       cached = cache[current]
       break;
     } else {
       current--
     }
   }
   const thisTotal = cached + num * (index - current)
   cache[index] = thisTotal
   total += thisTotal
 })
      return total
    }