Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk).

Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost.


-----Input-----

The first line contains an integer n (1 ≤ n ≤ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right.


-----Output-----

Print a single integer, the minimum amount of lost milk.

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.


-----Examples-----
Input
4
0 0 1 0

Output
1
Input
5
1 0 1 0 1

Output
3


-----Note-----

In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Data structures

Greedy algorithms


Solutions:
from collections import *
from itertools import accumulate

def arr_sum(arr):
	arr.appendleft(0)
	return list(accumulate(arr, lambda x, y: x + y))

def solve():
	(ans1, ans0) = (0, 0)
	for i in range(n):
		if a[i]:
			ans1 += n - (i + 1) - (mem[-1] - mem[i + 1])
		else:
			ans0 += mem[i]
	return min(ans0, ans1)
(n, a) = (int(input()), deque(map(int, input().split())))
mem = arr_sum(a.copy())
print(solve())


n = int(input())
cnt = 0
s = 0
l = [int(i) for i in input().split()]
for i in range(n):
	if l[i] == 1:
		cnt += 1
	else:
		s += cnt
print(s)


n = int(input())
lst = list(map(int, input().split()))
dp_0 = [0 for i in range(n)]
dp_1 = [0 for i in range(n)]
count_1 = 0
for i in range(n):
	dp_1[i] = count_1
	count_1 += lst[i]
count_0 = 0
for i in range(n - 1, -1, -1):
	dp_0[i] = count_0
	if lst[i] == 0:
		count_0 += 1
num_0 = 0
num_1 = 0
total_0 = 0
total_1 = 0
for i in range(n):
	if lst[i] == 1:
		total_1 = total_1 + dp_0[i] + dp_1[i]
		total_1 = total_1 - (n - i - 1 - dp_0[i])
	else:
		total_0 = total_0 + dp_0[i] + dp_1[i]
		total_0 = total_0 - (i - dp_1[i])
print(min(total_1, total_0))


ans = 0
cnt = 0
N = input()
t = input().split()
for i in t:
	if int(i) == 1:
		cnt += 1
	else:
		ans += cnt
print(ans)


from sys import stdin, stdout
nmbr = lambda : int(stdin.readline())
lst = lambda : list(map(int, stdin.readline().split()))
for _ in range(1):
	n = nmbr()
	a = lst()
	sm = 0
	looking_l = [0] * (1 + n)
	for i in range(n - 1, -1, -1):
		if a[i] == 0:
			looking_l[i] = 1 + looking_l[i + 1]
		else:
			looking_l[i] = looking_l[i + 1]
	for i in range(n):
		if a[i] == 1:
			sm += looking_l[i]
	print(sm)


def main():
	input()
	i = res = 0
	for c in input()[::2]:
		if c == '1':
			i += 1
		else:
			res += i
	print(res)

def __starting_point():
	main()
__starting_point()


n = int(input())
l = [int(i) for i in input().split()]
zeroes = [0] * n
if l[-1] == 0:
	zeroes[n - 1] = 1
else:
	zeroes[n - 1] = 0
for i in range(n - 2, -1, -1):
	zeroes[i] = zeroes[i + 1]
	if l[i] == 0:
		zeroes[i] += 1
cnt = 0
for i in range(n):
	if l[i] == 1:
		cnt += zeroes[i]
print(cnt)


n = int(input())
s = k = 0
for i in input()[::2]:
	if i == '1':
		k += 1
	else:
		s += k
print(s)


n = int(input())
lis = list(map(int, input().split()))
(oc, tc) = (0, 0)
for i in lis:
	if i == 1:
		oc += 1
	elif i == 0:
		tc += oc
val1 = tc
(tc, oc) = (0, 0)
for i in reversed(lis):
	if i == 0:
		oc += 1
	elif i == 1:
		tc += oc
val2 = tc
print(min([val1, val2]))


n = int(input())
a = [int(x) for x in input().split()]
ans = 0
r = 0
for x in a:
	if x == 1:
		r += 1
	else:
		ans += r
print(ans)


n = input()
a = [int(x) for x in input().split()]
ans = sum = 0
for i in a:
	if i == 1:
		sum += 1
	else:
		ans += sum
print(ans)


n = int(input())
nm = [0] + list(map(int, input().split())) + [-1]
sm = [0] * len(nm)
for i in range(n + 1, 0, -1):
	if not nm[i]:
		sm[i - 1] = sm[i] + 1
	else:
		sm[i - 1] = sm[i]
ans = 0
for i in range(1, n + 1):
	if nm[i]:
		ans += sm[i]
print(str(ans))


n = int(input())
a = list(map(int, input().split()))
b = [0, 0]
ans = 0
for i in range(n):
	b[a[i]] += 1
	if a[i] == 0:
		ans += b[1]
print(ans)


n = int(input())
z = [int(x) for x in input().split()]
x = z.count(0)
an = 0
for i in range(n):
	if z[i] == 0:
		x -= 1
	else:
		an += x
x = z.count(1)
z = z[::-1]
an12 = 0
for i in range(n):
	if z[i] == 1:
		x -= 1
	else:
		an12 += x
print(min(an, an12))


n = int(input())
m = list(input().split())
ca = cb = int(0)
for i in range(n):
	if int(m[i]) == 0:
		ca += 1
	else:
		cb += 1
a = b = int(0)
if ca > cb:
	for i in range(n):
		if int(m[n - i - 1]) == 0:
			a = a + cb
		else:
			cb -= 1
	print(a)
else:
	for i in range(n):
		if int(m[i]) == 1:
			b += ca
		else:
			ca -= 1
	print(b)


import sys
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
input = sys.stdin.readline
t = 1
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	(zero, one) = (0, 0)
	(z, o) = (0, 0)
	for i in range(n):
		if a[i] == 0:
			zero += z
		else:
			z += 1
	for i in range(n - 1, -1, -1):
		if a[i] == 1:
			one += o
		else:
			o += 1
	print(min(zero, one))


def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	count_arr = [0] * n
	zero_count = 0
	for _ in range(n):
		if arr[n - _ - 1] == 0:
			zero_count += 1
		count_arr[n - _ - 1] = zero_count
	ans = 0
	for _ in range(n):
		if arr[_] == 1:
			ans += count_arr[_]
	print(ans)
solve()


n = int(input())
lis = list(map(int, input().split()))
ans = b = 0
for i in range(n - 1, -1, -1):
	if lis[i] == 1:
		ans += b
	else:
		b += 1
print(ans)


n = int(input())
array = list(map(int, input().split()))
cnt = 0
s = [0 for i in range(n)]
s[n - 1] = 1 * (array[n - 1] == 0)
for i in range(n - 2, -1, -1):
	s[i] = s[i + 1] + 1 * (array[i] == 0)
for i in range(n):
	cnt += s[i] * (array[i] == 1)
print(cnt)


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
cnt0 = [0] * n
for i in range(n):
	if a[i] == 0:
		cnt0[i] += 1
for i in range(n - 1)[::-1]:
	cnt0[i] += cnt0[i + 1]
ans = 0
for i in range(n):
	if a[i] == 1:
		ans += cnt0[i]
print(ans)


n = int(input())
l = list(map(int, input().split()))
x = l.count(0)
y = l.count(1)
if x >= y:
	a = []
	k = 0
	for i in range(n - 1, -1, -1):
		if l[i] == 1:
			a.append(k)
		else:
			k += 1
else:
	a = []
	k = 0
	for i in range(n):
		if l[i] == 0:
			a.append(k)
		else:
			k += 1
print(sum(a))


n = int(input())
ls = list(map(int, input().split()))
cnt = 0
ans = 0
for i in ls:
	if i == 0:
		ans += cnt
	elif i == 1:
		cnt += 1
print(ans)


x = 0
y = 0
n = int(input())
a1 = input().split()
a = [int(a1[i]) for i in range(len(a1))]
for i in range(n):
	if a[i]:
		x += 1
	else:
		y += x
print(y)


import sys
n = int(input())
L = list(map(int, sys.stdin.readline().split()))
z = L.count(0)
if z == n or z == 0:
	print(0)
	Zeros = [0] * n
	Zeros[n - 1] += 1 - L[n - 1]
	for i in range(n - 2, -1, -1):
		Zeros[i] = Zeros[i + 1]
		if L[i] == 0:
			Zeros[i] += 1
else:
	Zeros = [0] * n
	Zeros[n - 1] += 1 - L[n - 1]
	for i in range(n - 2, -1, -1):
		Zeros[i] = Zeros[i + 1]
		if L[i] == 0:
			Zeros[i] += 1
	Ans = 0
	o = 0
	z = 0
	p = L[0]
	if L[0] == 1:
		o += 1
	for i in range(1, n):
		if L[i] == p:
			if p == 1:
				o += 1
		elif L[i] == 0:
			Ans += Zeros[i] * o
			p = 0
		else:
			o = 1
			p = 1
	print(Ans)


from __future__ import print_function
from collections import *
from sys import stdin
from bisect import *
from heapq import *
from math import log2
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
rr = lambda x: reversed(range(x))
mod = int(1000000000.0) + 7
inf = float('inf')
(n,) = gil()
a = gil()
ans = inf
z = 0
tmp = 0
for i in reversed(range(n)):
	if a[i]:
		tmp += z
	else:
		z += 1
o = 0
ans = min(ans, tmp)
tmp = 0
for i in range(n):
	if a[i]:
		o += 1
	else:
		tmp += o
print(min(ans, tmp))


n = int(input())
arr = list(map(int, input().split()))
(cnt_zero, res) = (0, 0)
for i in range(n - 1, -1, -1):
	if arr[i] == 0:
		cnt_zero += 1
	else:
		res += cnt_zero
print(res)


def cows(n, lst):
	(zeros, result) = (0, 0)
	for i in range(n - 1, -1, -1):
		if lst[i] == 0:
			zeros += 1
		else:
			result += zeros
	return result
m = int(input())
a = [int(j) for j in input().split()]
print(cows(m, a))


n = int(input())
a = list(map(int, input().split()))
res = 0
b = 0
for i in reversed(range(n)):
	if a[i] == 1:
		res += b
	else:
		b += 1
print(res)


n = int(input())
arr = list(map(int, input().split()))
zero_count = [0] * n
zero_count[n - 1] = int(arr[n - 1] == 0)
for i in reversed(range(n - 1)):
	zero_count[i] = zero_count[i + 1] + int(arr[i] == 0)
res = 0
for i in range(n - 1):
	if arr[i] == 1:
		res += zero_count[i + 1]
print(res)


n = input()
a = list(map(int, input().split(' ')))
n0 = a.count(0)
n1 = a.count(1)
res = list()
if n0 < n1:
	n1_cnt = 0
	for i in a:
		if i == 1:
			n1_cnt += 1
		else:
			res.append(n1_cnt)
else:
	n0_cnt = 0
	for i in reversed(a):
		if i == 0:
			n0_cnt += 1
		else:
			res.append(n0_cnt)
print(sum(res))


import fileinput
for line in fileinput.input():
	x = [int(i) for i in line.split()]
rightCows = 0
total = 0
for i in range(0, len(x)):
	if x[i]:
		rightCows += 1
	else:
		total += rightCows
print(total)


import sys

def solve(cows):
	n = len(cows)
	num_vis = [0] * n
	facing = [0] * n
	left_inds = []
	right_inds = []
	for i in range(n):
		if cows[i] == 0:
			left_inds.append(i)
			num_vis[i] = max(i, 0)
			facing[i] = len(right_inds)
		else:
			right_inds.append(i)
			num_vis[i] = max(n - i - 1, 0)
	nleft = 0
	for i in range(n - 1, -1, -1):
		if cows[i] == 1:
			facing[i] = nleft
		else:
			nleft += 1
	(milked_left_facing, milked_right_facing, tot_lost) = (0, 0, 0)
	while milked_left_facing + milked_right_facing < n:
		if len(right_inds) == 0 or len(left_inds) == 0:
			break
		if num_vis[right_inds[0]] - milked_right_facing > num_vis[left_inds[-1]] - milked_left_facing:
			ri = right_inds.pop(0)
			milked_right_facing += 1
			milk_lost = max(facing[ri] - milked_left_facing, 0)
			tot_lost += milk_lost
			if len(right_inds) == 0:
				break
		else:
			li = left_inds.pop(-1)
			milked_left_facing += 1
			milk_lost = max(facing[li] - milked_right_facing, 0)
			tot_lost += milk_lost
			if len(left_inds) == 0:
				break
	return tot_lost
IN = [x.strip() for x in sys.stdin.readlines()]
Q = int(IN[0])
cows = [int(x) for x in IN[1].split(' ')]
print(solve(cows))


n = int(input())
arr = list(map(int, input().split()))
cnt = [0] * n
cnt[0] = arr[0]
for i in range(1, n):
	cnt[i] = cnt[i - 1] + arr[i]
ans = 0
for i in range(n):
	if arr[i] == 0:
		ans += cnt[i]
print(ans)


__author__ = 'Pavel Mavrin'
n = int(input())
a = [int(x) for x in input().split()]
s = 0
res = 0
for i in a:
	if i == 0:
		res += s
	else:
		s += 1
print(res)


a = int(input())
z = list(map(int, input().split()))
ans = [z[0]]
for i in range(1, len(z)):
	ans.append(ans[-1] + z[i])
score = 0
for i in range(len(z)):
	if z[i] == 0:
		score += ans[i]
print(score)


from collections import *
from math import *
n = int(input())
a = list(map(int, input().split()))
(ct0, res0) = (0, 0)
for i in range(n):
	if a[i] == 1:
		ct0 += 1
	else:
		res0 += ct0
(ct1, res1) = (0, 0)
for i in range(n - 1, -1, -1):
	if a[i] == 0:
		ct1 += 1
	else:
		res1 += ct1
print(min(res0, res1))


def P0122_cow_milk(n, a):
	ans = 0
	r = 0
	for x in a:
		if x == 1:
			r += 1
		else:
			ans += r
	return ans
n = int(input())
a = [int(x) for x in input().split()]
result = P0122_cow_milk(n, a)
print(result)


(n, v) = (int(input()), list(map(int, input().split())))
ans = 0
r = 0
for i in v:
	if i == 1:
		r += 1
	else:
		ans += r
print(ans)


n = int(input())
arr = list(map(int, input().split()))
dif = [0] * n
l = 0
for i in range(n - 1, -1, -1):
	if arr[i] == 0:
		l += 1
	dif[i] = l
nuksan = 0
for i in range(n - 1):
	if arr[i] == 1:
		nuksan += dif[i + 1]
print(nuksan)


n = int(input())
a = list(map(int, input().split()))
b = 0
c = 0
d = 0
e = 0
for i in range(n):
	b += a[i]
	c += b * (1 - a[i])
for i in range(n - 1, -1, -1):
	d += 1 - a[i]
	e += d * a[i]
print(min(c, e))


def cows(lst):
	(k1, k2) = (0, 0)
	for i in range(len(lst)):
		if lst[i] == 1:
			k1 += 1
		else:
			k2 += k1
	return k2
n = int(input())
a = [int(j) for j in input().split()]
print(cows(a))