import sys #input = sys.stdin.readline #文字列につけてはダメ input = sys.stdin.buffer.readline #文字列につけてはダメ #sys.setrecursionlimit(1000000) #import bisect #import itertools #import random #from heapq import heapify, heappop, heappush #from collections import defaultdict #from collections import deque #import copy #かなり実装難しい。★2ではないでしょ。 def main(): N = int(input()) A = [0] + list(map(int,input().split())) B = [] ans = 0 temp = [] for i in range(1,N+1): if A[i] == 0: if A[i-1] == 1: temp.append(A[i]) else: B.append(temp) temp = [] else: if A[i-1] == 0: temp.append(A[i]) else: B.append(temp) temp = [A[i]] if temp: if temp[-1] == 0: ans -= len(temp)//2 else: ans -= (len(temp)+1)//2 B.append(temp) #print(ans) for i in range(len(B)): if len(B[i])%2 == 1: x = (len(B[i])+1)//2 ans += x**2 else: x = len(B[i])//2 ans += (1+x)*x #print(ans) print(ans) if __name__ == '__main__': main()