n = int(input()) a = list(map(int, input().split())) a.append(0) s = a[0] k = 0 nc = 0 for i in range(n + 1): if s == 0 and a[i] == 0: nc += (i - k)*(i - k + 1)/2 s = 1 continue if s == 1 and a[i] == 1: k = i s = 0 print(int(n * (n + 1) / 2 - nc))