N = int(input()) A = list(map(int, input().split())) + [0] Len, Ans = 0, N * (N + 1) // 2 for i in range(N + 1): if A[i] == 0: Ans -= Len * (Len + 1) // 2 Len = 0 else: Len += 1 print(Ans)