n = int(input()) a = list(map(int, input().split())) a_reverse = a[::-1] num = [0]*n check = [True]*n cnt = 0 ans = 0 for i in range(n-1): # cnt = 0 if a_reverse[i] != a_reverse[i + 1]: cnt += 1 check[i + 1] = check[i] else: cnt = 0 check[i + 1] = False if a_reverse[i + 1]: num[i + 1] = cnt for i in range(1, n): if a_reverse[i] and not check[i]: ans += 1 # print(*a_reverse) # print(*num) # print(*check) print(sum(num) + ans)