def main(): N = int(input()) B = list(map(int, input().split())) m = 0 cnt = 0 for i in range(N): if B[i] >= 1: continue for j in range(m,N): if B[j] > 1: B[i] += 1 B[j] -= 1 cnt += abs(i-j) if B[j] > 1: m = j else: m = j + 1 break print(cnt) if __name__ == "__main__": main()