# たとえば00140 # 左から貪欲法で動かせばいい # Bを見ていって、数字があったら、Aの左から埋めていく # ABC004Dと近いかもしれない # BiをAjからAkに配るときのムーブ数を出力するdefがあるといい def move_to_A(i, j, k): if j <= i and k <= i: dist1 = abs(i-j) dist2 = abs(i-k) if dist1 == 0 and dist2 == 0: move = 0 elif dist2 == 0: move = dist1*(dist1+1)//2 else: move = dist1*(dist1+1)//2 - (dist2-1)*dist2//2 elif i <= j and i <= k: dist1 = abs(k-i) dist2 = abs(j-i) if dist1 == 0 and dist2 == 0: move = 0 elif dist2 == 0: move = dist1*(dist1+1)//2 else: move = dist1*(dist1+1)//2 - (dist2-1)*dist2//2 else: # つまり j < i and i < k dist1 = abs(i-j) dist2 = abs(k-i) move = dist1*(dist1+1)//2 + dist2*(dist2+1)//2 return move # test # move_to_A(3, 2, 4) N = int(input()) B = list(map(int, input().split())) left = 0 right = 0 ans = 0 for i in range(N): if B[i] > 0: right = left + B[i] - 1 calc = move_to_A(i, left, right) ans += calc left = right +1 right = left #print(i, B[i], 'calc', calc, 'ans', ans, left, right) print(ans)