output_description: Print the answer.
  
  Note that the answer may not fit into a 32-bit integer.
  
  * * *
input_description: The input is given from Standard Input in the following format:
  
      
      
      N
      a_1 a_2 ... a_N
attempt: # AGC005 B
  
  N = int(input())
  
  A = list(map(int, input().split()))[::-1]
  
  MIN = A[0]
  BACK = A[0]
  type2 = 1
  BACKSUM = A[0]
  
  ans = A[0]
  for i in range(1, N):
      if A[i] < MIN:
          MIN = A[i]
          ans += MIN * (i + 1)
          BACKSUM = MIN * (i + 1)
          type2 = 1
      else:
          if A[i] > BACK:
              ans += A[i] + BACKSUM
              BACKSUM = A[i] + BACKSUM
              type2 = 1
          else:
              ans += A[i] + BACKSUM - ((BACK - A[i]) * type2)
              BACKSUM = A[i] + BACKSUM - ((BACK - A[i]) * type2)
              type2 += 1
      # print(BACKSUM)
      BACK = A[i]
  
  print(ans)
  
problem_description: Statement
  
  One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from
  his friend.
  
  Find the following:
  
  ![](https://atcoder.jp/img/agc005/520049e1a049bb9810b398b35d7dcb7f.png)
samples: [{"input": "3\n    2 1 3", "output": "9\n    \n\n* * *"}, {"input": "4\n    1 3 2 4", "output": "19\n    \n\n* * *"}, {"input": "8\n    5 4 8 1 2 6 7 3", "output": "85"}]