n = int(input()) b = [int(i) for i in input().split()] now = 0 ans = 0 for i in range(n): if b[i] == 0: for j in range(n): if b[now] >= 2: b[now] -= 1 b[i] = 1 #print(now-i,"now") ans += abs(now-i) break else: now += 1 print(ans)