N = int(input()) A = list(map(int,input().split())) L = 0 cand = [] flg = 0 for i in range(N): if A[i] == 1 and L == 0: st = i L += 1 flg = 1 elif A[i] == 0: if flg == 1: flg = 0 L += 1 else: cand.append(L) L = 0 elif A[i] == 1 and flg == 0: L += 1 flg = 1 else: st = i cand.append(L) L = 1 flg = 1 cand.append(L-1) ans = 0 for i in cand: if i%2 == 1: ans += ((i+1)//2)**2 else: m = i//2 ans += m*(m+1) print(ans) #print(cand)