Over time, Alexey's mail box got littered with too many letters. Some of them are read, while others are unread.

Alexey's mail program can either show a list of all letters or show the content of a single letter. As soon as the program shows the content of an unread letter, it becomes read letter (if the program shows the content of a read letter nothing happens). In one click he can do any of the following operations: Move from the list of letters to the content of any single letter. Return to the list of letters from single letter viewing mode. In single letter viewing mode, move to the next or to the previous letter in the list. You cannot move from the first letter to the previous one or from the last letter to the next one.

The program cannot delete the letters from the list or rearrange them.

Alexey wants to read all the unread letters and go watch football. Now he is viewing the list of all letters and for each letter he can see if it is read or unread. What minimum number of operations does Alexey need to perform to read all unread letters?


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of letters in the mailbox.

The second line contains n space-separated integers (zeros and ones) — the state of the letter list. The i-th number equals either 1, if the i-th number is unread, or 0, if the i-th letter is read.


-----Output-----

Print a single number — the minimum number of operations needed to make all the letters read.


-----Examples-----
Input
5
0 1 0 1 0

Output
3

Input
5
1 1 0 0 1

Output
4

Input
2
0 0

Output
0



-----Note-----

In the first sample Alexey needs three operations to cope with the task: open the second letter, move to the third one, move to the fourth one.

In the second sample the action plan: open the first letter, move to the second letter, return to the list, open the fifth letter.

In the third sample all letters are already read.

Source: codeforces | Difficulty: EASY
Tags: Implementation


Solutions:
(a, b) = (input(), list(map(int, input().split())))
if 1 in b:
	(c, d) = (1, b.index(1))
	b[d] = 0
	while 1 in b:
		if b[d + 1] == 1:
			c += 1
			d += 1
			b[d] = 0
		else:
			c += 2
			d = b.index(1)
			b[d] = 0
	print(c)
else:
	print(0)


(n, s) = (int(input()), ''.join(input().split()))
(res, s) = (s.count('1'), s.strip('0'))
s = s.strip('1')
res += len([x for x in s.split('1') if '0' in x])
print(res)


import math
import sys
input = sys.stdin.readline

def inp():
	return int(input())

def invr():
	return list(map(int, input().split()))
n = inp()
l = invr()
if not 1 in l:
	print(0)
	sys.exit(0)
if not 0 in l:
	print(n)
	sys.exit(0)
i = 0
s = 0
ll = [0]
ad = 0
while s < len(l):
	e = l[s]
	if e:
		ad = 1
		ll[len(ll) - 1] += 1
	elif ad:
		ll.append(0)
		ad = 0
	s += 1
if not ll[len(ll) - 1]:
	ll.pop()
print(sum(ll) + len(ll) - 1)


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
cat = ''.join
catn = '\n'.join
mod = 1000000007
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
n = ri()
a = ris()
ans = 0
f = 1
for x in a:
	if x == 1:
		ans += 1
		f = 0
	elif f == 0:
		ans += 1
		f = 1
if ans and f:
	ans -= 1
print(ans)


n = int(input()) - 1
m = [i for i in ''.join(input().split()).split('0') if i != '']
ct = 0
if len(m) != 0:
	ct -= 1
	for i in m:
		ct += len(i) + 1
print(ct)


import re

def main():
	input()
	s = re.sub('00+', '0', input().replace(' ', '').strip('0'))
	print(len(s))
main()


j = int(input())
y = input()
x = [int(i) for i in y if not i == ' ']
k = 0
while k < j and x[k] == 0:
	k += 1
a = 1
if k == j:
	a = 0
f = False
for i in range(k + 1, j):
	if x[i] == 1:
		if f:
			a += 2
			f = False
		else:
			a += 1
	else:
		f = True
		while x[i] == 0:
			if i == j - 1:
				break
			i += 1
print(a)


import sys
import math
import collections
import bisect

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()
for t in range(1):
	n = int(input())
	arr = get_list()
	counter = dict([])
	for i in range(n):
		if arr[i] in counter:
			counter[arr[i]].append(i)
		else:
			counter[arr[i]] = [i]
	ans = 0
	for i in counter:
		if i == 0:
			continue
		row = counter[i]
		ans += 1
		for i in range(1, len(row)):
			gap = row[i] - row[i - 1]
			ans += min(gap, 2)
	print(ans)


n = int(input())
states = input().split()
op = 0
carry = 0
for i in range(0, n):
	if states[i] == '1':
		op = op + 1 + carry
		carry = 0
		if i + 1 < n:
			if states[i + 1] == '1':
				continue
			else:
				carry = 1
print(op)


import sys
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().split()))
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
a = int_array()
ans = 0
unread = a.count(1)
prev = 0
for i in range(n):
	if a[i] == 1:
		ans += 1
		prev = 1
		unread -= 1
		if unread == 0:
			break
	elif prev == 1:
		ans += 1
		prev = 0
print(ans)


def main():
	n = input()
	letters = list(input().split(' '))
	idx = list()
	for i in range(len(letters)):
		if letters[i] == '1':
			idx.append(i)
	m = len(idx)
	if m == 0:
		print(0)
		return
	res = 1
	for i in range(1, m):
		res += min(2, idx[i] - idx[i - 1])
	print(res)

def __starting_point():
	main()
__starting_point()


s = 0
p = 0
for (i, j) in zip(range(int(input())), map(int, input().split())):
	if j:
		if s == 0:
			s = 1
			p = i
		else:
			s += min(2, i - p)
			p = i
print(s)


n = int(input())
s = ''.join([i for i in input().split()])
a = s.count('1')
b = s.count('10')
if s[-1] == '1':
	b += 1
a -= b
b = b * 2
if b >= 1:
	print(b + a - 1)
else:
	print(0)


n = int(input())
a = ['0'] + input().split() + ['0']
res = 0
for i in range(1, n + 1):
	if a[i] == '0':
		continue
	res += 1
	if a[i + 1] == '0':
		res += 1
print(res - 1 if res > 0 else res)


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


num_letters = int(input())
letters = [x.strip() for x in input().split('0') if x.strip() != '']
count = -1
for i in letters:
	count += i.count('1')
	count += 1
print(count if count != -1 else 0)


n = int(input())
m = [int(x) for x in input().split(' ')]
ans = sum(m)
m = m[::-1]
if ans > 0:
	m = m[m.index(1):]
m = m[::-1]
for i in range(len(m)):
	if i == 0:
		curr = m[i]
		past = m[i]
	else:
		curr = m[i]
	if past == 1 and curr == 0:
		ans += 1
	past = m[i]
print(ans)


total = int(input())
t = list(map(int, input().split()))
no = t.count(1)
d = []
if no == 0:
	print('0')
	exit(0)
for i in range(total):
	if t[i] == 1:
		d += [i]
sum = 1
for i in range(no - 1):
	dif = d[i + 1] - d[i]
	if dif == 1:
		sum += 1
	else:
		sum += min(dif, 2)
print(sum)


__author__ = '$'
import sys
try:
	while True:
		n = int(input())
		val = list(map(int, input().split(' ')))
		vec = []
		for i in range(n):
			if val[i]:
				vec.append(i)
		res = 1 if len(vec) else 0
		for i in range(1, len(vec), 1):
			res += 2 if vec[i] != vec[i - 1] + 1 else 1
		print(res)
except EOFError:
	pass


n = int(input())
l = list(map(int, input().split()))
co = 0
i = 0
while i < n:
	if l[i] == 1:
		l[i] = 0
		co += 1
		if 1 in l and l.index(1) - i > 1:
			i = l.index(1)
			co += 1
	else:
		i += 1
print(co)


input()
print(max(0, sum((len(s) + 1 for s in ''.join(input().split()).split('0') if s)) - 1))


n = int(input())
bits = ''.join(input().split(' '))
pre = -1
count = 0
for i in range(0, n):
	if bits[i] == '1':
		if i - pre <= 2 and pre != -1:
			count += i - pre
		elif pre == -1:
			count += 1
		else:
			count += 2
		pre = i
print(count)


n = int(input())
arr = list(map(int, input().split())) + [0]
res = []
k = 0
for i in arr:
	if i == 1:
		k += 1
	else:
		if k > 0:
			res.append(k)
		k = 0
if len(res) > 0:
	print(sum(res) + len(res) - 1)
else:
	print(0)


import sys

def main():
	fin = sys.stdin
	fout = sys.stdout
	n = int(fin.readline())
	A = list(map(int, fin.readline().split()))
	ans = 0
	inSeg = False
	for i in range(n):
		if A[i]:
			ans += 1
			inSeg = True
		elif inSeg:
			ans += 1
			inSeg = False
	if A[-1] == 0:
		ans = max(0, ans - 1)
	print(ans)
	fin.close()
	fout.close()
main()


start = []
contents = []
count = 1
while True:
	try:
		line = input()
	except EOFError:
		break
	if count == 1:
		start.append(line.split())
		count = 0
	else:
		contents.append(line.split())
index = int(start[0][0])
maillist = contents[0][0:]
skipflag = 0
count = 0
last1 = 0
for i in range(0, index):
	if maillist[i] == '1' and skipflag == 0:
		count += 1
		skipflag = 1
		last1 = 0
	elif maillist[i] == '1' and skipflag == 1:
		count += 1
		last1 = 0
	elif maillist[i] == '0' and skipflag == 1:
		skipflag = 0
		count += 1
		last1 = 1
	elif maillist[i] == '0' and skipflag == 0:
		skipflag = 0
if last1 == 1:
	count -= 1
print(count)


n = int(input())
l = list(map(int, input().split()))
c = 0
if l.count(1) > 0:
	d = l.index(1)
	c += 1
	for i in range(d + 1, n):
		if l[i] == 1 and l[i - 1] == 1:
			c += 1
		if l[i] == 1 and l[i - 1] == 0:
			c += 2
print(c)


def arr_inp():
	return [int(x) for x in input().split()]
(n, a, ans) = (int(input()), arr_inp(), 0)
for i in range(n):
	if a[i]:
		ans += 1
		if ans > 1 and (i > 0 and a[i - 1] == 0):
			ans += 1
print(ans)


n = int(input())
letter = str(input())
if '1' not in letter:
	print(0)
	quit()
if '1 1' not in letter:
	print(2 * letter.count('1') - 1)
	quit()
op = 2 * letter.count('1') - 1
for i in range(n - 1):
	if letter[2 * i] == '1' and letter[2 * i + 2] == '1':
		op -= 1
print(op)


n = int(input())
l = [int(i) for i in input().split()]
if sum(l) == n:
	print(n)
elif sum(l) == 0:
	print(0)
else:
	ashok = 0
	c = 0
	zeros = 0
	karma = 0
	ans = l.count(1)
	for i in range(n):
		if l[i] == 0:
			ashok += 100
			zeros += 1
		elif karma == 0:
			karma = 1
			zeros = 0
		elif zeros:
			ans += 1
			zeros = 0
	print(ans)


n = int(input())
x = list(map(int, input().split()))
current_state = 0
answer = 0
for i in range(n):
	if x[i] == 0 and current_state == 0:
		continue
	if x[i] == 0:
		current_state = 0
		answer += 1
	if x[i] != 0:
		answer += 1
		current_state = 1
if x[n - 1] == 0 and answer != 0:
	answer -= 1
print(answer)


n = int(input())
a = [int(i) for i in input().split()]
r = 0
b = False
for i in a:
	if i == 0:
		r += 1 if b else 0
		b = False
	else:
		r += 1
		b = True
print(r if b else max(r - 1, 0))


n = int(input())
c = 0
z = 0
s = input()
for i in s:
	if i == '1':
		c += 1
		if z:
			c += 1
			z = 0
	elif i == '0' and c > 0:
		z = 1
print(c)


cartas = int(input())
caixa = input().split()
operacoes = 0
cont = 0
boolean = False
for i in range(len(caixa)):
	if caixa[i] == '1':
		boolean = True
		if cont == 1 or cont > 1:
			operacoes += 1
		cont = 0
		operacoes += 1
	if caixa[i] == '0' and boolean:
		cont += 1
print(operacoes)


import sys
sys.setrecursionlimit(2100)
n = int(input())
seq = list(map(int, input().split()))
pd = []
ultimo = -1
primeiro = -1
for i in range(n):
	pd.append([-1, -1])
	if seq[i] == 1:
		ultimo = i

def solve(i, e):
	if i == n:
		return 0
	if i == ultimo and e == 1:
		return 0
	if pd[i][e] != -1:
		return pd[i][e]
	if e == 0:
		if seq[i] == 0:
			pd[i][e] = solve(i + 1, 0)
			return pd[i][e]
		if seq[i] == 1:
			pd[i][e] = 1 + solve(i, 1)
			return pd[i][e]
	if e == 1:
		sai = 1 + solve(i + 1, 0)
		prox = 1 + solve(i + 1, 1)
		pd[i][e] = min(sai, prox)
		return pd[i][e]
print(solve(0, 0))


n = int(input())
l = ''.join(input().split())
l = l.strip('0')
divs = 0
for (a, b) in zip(l[:-1], l[1:]):
	if a == '1' and b == '0':
		divs += 1
if l.count('1') == 0:
	print(0)
elif l.count('0') == 0:
	print(len(l))
else:
	print(l.count('1') + divs)


n = int(input())
a = list(map(int, input().strip().split(' ')))
a = [0] + a
ans = 0
for i in range(1, n + 1):
	if a[i] and a[i - 1]:
		ans += 1
	elif a[i]:
		ans += 2
if ans:
	print(ans - 1)
else:
	print(0)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def main():
	n = I()
	a = LI()
	t = -inf
	r = 0
	for c in a:
		if c == 1:
			if t > 0:
				r += min(t, 2)
			else:
				r += 1
			t = 1
		else:
			t += 1
	return r
print(main())


num = input()
mails = input()
mails = mails.split(' ')
moves = 0
first = True
last_mail = 0
for mail in mails:
	mail = int(mail)
	if mail == 1:
		if first:
			moves += 1
			last_mail = 1
			first = False
		elif last_mail == 1:
			moves += 1
		elif last_mail == 0:
			moves += 2
			last_mail = 1
	else:
		last_mail = 0
print(moves)


n = int(input())
a = list(map(int, input().split()))
prev = 1
ans = 0
for x in a:
	if x == 1:
		if ans == 0 or prev == 1:
			ans += 1
		else:
			ans += 2
	prev = x
print(ans)


import math
import os
import random
import re
import sys
import functools
from operator import itemgetter, attrgetter
from collections import Counter
Y = lambda : list(map(int, input().split()))
N = lambda : int(input())
n = N()
a = Y()
(i, j) = (0, n - 1)
while i < j and (not (a[i] and a[j])):
	if not a[i]:
		i += 1
	if not a[j]:
		j -= 1
(f, cnt) = (0, a[i:j + 1].count(1))
for i in a[i:j + 1]:
	if not i:
		f = 1
	elif f:
		cnt += 1
		f = 0
print(cnt)


n = input()
letters = list(map(int, input().split()))
(op, interval) = (0, 0)
index = 0
while index < len(letters) and letters[index] != 1:
	index += 1
for i in range(index, len(letters)):
	if letters[i] == 1:
		if interval > 0:
			op += 2
		else:
			op += 1
		interval = 0
	else:
		interval += 1
print(op)


n = int(input())
a = list(map(int, input().split()))
cnt = 0
c = 0
pos = -1
while cnt < len(a):
	if a[cnt]:
		c += 1
		if pos == -1:
			pos = cnt
	cnt += 1
res = 0
cnt = 1
while c:
	if a[pos] == 1:
		if cnt <= 2:
			res += cnt
		else:
			res += 2
		cnt = 0
		c -= 1
	pos += 1
	cnt += 1
print(res)


n = int(input())
x = list(map(int, input().split()))
if x.count(1) != 0:
	lastIndex = n - x[::-1].index(1) - 1
cnt = 0
for i in range(n):
	if i == n - 1:
		if x[i] == 1:
			cnt += 1
			break
	if x[i] == 1:
		cnt += 1
		if x[i + 1] == 0 and i != lastIndex:
			cnt += 1
print(cnt)


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


n = int(input())
sta = [i for i in ''.join(input().split()).split('0') if i != '']
ans = 0
for i in sta:
	ans += 2 + len(i) - 1
if ans != 0:
	print(ans - 1)
else:
	print(0)


n = int(input())
l = list(map(int, input().split()))
if not 1 in l:
	print(0)
else:
	j = l.index(1)
	ans = 1
	for i in range(j + 1, n):
		if l[i] == 1:
			ans += min(i - j, 2)
			j = i
	print(ans)


def main():
	n = input()
	n = int(n)
	a = list(map(int, input().split(' ')))
	a = list(a)
	if 0 in a:
		if len(a) == a.count(0):
			print(0)
			return
		pass
	pass
	a = list(map(str, a))
	a = ''.join(a)
	a = a.split('0')
	s = 0
	for x in a:
		s += len(x)
	pass
	z = len(a) - a.count('')
	print(s + z - 1)
	pass
main()


n = int(input())
lts = [int(x) for x in input().strip().split()]
ones = [i for (i, j) in enumerate(lts) if j == 1]
ct = 0
if len(ones) == 0:
	print(0)
elif len(ones) == 1:
	print(1)
else:
	ct = 1
	for i in range(len(ones) - 1):
		if ones[i + 1] == ones[i] + 1:
			ct = ct + 1
		else:
			ct = ct + 2
	print(ct)


N = int(input())
L = list(map(int, input().split()))
sumv = sum(L)
ans = 0
now = 0
cnt = 0
while now < N:
	if L[now] == 0:
		now += 1
		continue
	ans += 1
	cnt += 1
	if cnt == sumv:
		break
	if now < N - 1:
		if L[now + 1] == 1:
			now += 1
		else:
			ans += 1
			now += 1
print(ans)


n = int(input())
sta = [int(i) for i in input().split()] + [0]
current = 0
ans = 0
while current < n:
	if sta[current] == 0:
		current += 1
	else:
		for i in range(current + 1, n + 1):
			if sta[i] == 1:
				ans += 1
			else:
				current = i + 1
				ans += 2
				break
if ans != 0:
	print(ans - 1)
else:
	print(0)


n = int(input())
c = list(map(int, input().split()))
l = 0
p = 0
for i in range(n):
	if c[i] == 1:
		if p == 0:
			p = 1
		l += 1
	elif p == 1:
		l += 1
		p = 0
if p == 0 and l != 0:
	l -= 1
print(l)


n = input()
lst = map(int, input().split())
seqs = 0
total = 0
prev = 0
for i in lst:
	if prev:
		if i:
			total += 1
		else:
			prev = 0
	elif i:
		prev = 1
		seqs += 1
		total += 1
if total:
	print(seqs + total - 1)
else:
	print(0)


__author__ = 'hamed1soleimani'
n = int(input())
items = input().split()
for x in range(len(items)):
	items[x] = int(items[x])
pointer = 0
state = 0
actions_number = 0
while True:
	if pointer == n:
		break
	if not state:
		if items[pointer]:
			state = 1
			actions_number += 1
	elif items[pointer]:
		actions_number += 1
		if not items[pointer - 1]:
			actions_number += 1
	pointer += 1
print(actions_number)


n = int(input())
s = [int(i) for i in input().split()]
(i, j) = (0, 0)
if s.count(1) > 0:
	cnt = 1
else:
	print(0)
	exit()
while i < n:
	if s[i] == 1:
		j = i + 1
		while j < n and s[j] != 1:
			j += 1
		if j == n:
			break
		if j - i >= 3:
			cnt += 2
		else:
			cnt += j - i
	i += 1
print(cnt)


n = int(input())
l = ''.join(input().split())
while '00' in l:
	l = l.replace('00', '0')
l = l.replace('0', ' ')
print(len(l.strip()))


num = int(input())
numbers = []
numbers = input().split()
count = 0
inside = False
zero = False
for i in numbers:
	if i == '1':
		if zero:
			count += 1
		count += 1
		zero = False
		continue
	if i == '0' and count != 0:
		zero = True
		continue
print(count)


import math
n = int(input())
array = list(map(int, input().split(' ')))
ar = []
total = 0
for i in range(n):
	if array[i] == 0:
		total += 1
	else:
		if total > 0:
			ar.append(total)
		total = 0
if array[0] == 1:
	pass
elif len(ar) > 0:
	ar.remove(ar[0])
totalread = 0
totalmove = len(ar)
for i in range(n):
	if array[i] == 1:
		totalread += 1
print(totalread + totalmove)


from sys import stderr
N = int(input())
data = [int(i) for i in input().split()]
data.append(0)
data.append(0)
data.append(0)
ans = 0
ins = 0
last = -1
for i in range(N):
	if data[i] == 1:
		last = i
if last == -1:
	print(0)
	exit()
for i in range(last + 1):
	if data[i] == 1 and ins == 0:
		ans += 1
		ins = 1
		continue
	if data[i] == 1 and ins == 1:
		ans += 1
		continue
	if data[i] == 0 and ins == 1:
		ans += 1
		if data[i + 1] == 0:
			ins = 0
		continue
print(ans)


n = int(input())
lis = input().split()
lis1 = []
for i in range(n):
	lis[i] = int(lis[i])
	if lis[i] == 1:
		lis1.append(i)
if len(lis1) == 0:
	print(0)
else:
	lis2 = [1 for k in range(len(lis1))]
	for i in range(1, len(lis1)):
		if lis1[i] - lis1[i - 1] == 1:
			lis2[i] = 1
		else:
			lis2[i] = 2
	print(sum(lis2))


(n, l) = (int(input()), list(map(int, input().split())))
if 1 not in l:
	print(0)
else:
	(a, c) = (l.index(1), 1)
	for x in range(n):
		if l[x] == 1:
			if x - a > 2:
				c += 2
			else:
				c += x - a
			a = x
	print(c)


n = int(input())
s = input().split()
spans = []
k = 0
if s[0] == '1':
	spans.append(1)
	k = k + 1
for i in range(1, n):
	if s[i] == '1' and s[i - 1] == '1':
		spans[k - 1] = spans[k - 1] + 1
	if s[i] == '1' and s[i - 1] == '0':
		spans.append(1)
		k = k + 1
ans = sum(spans) + len(spans) - 1
if len(spans) == 0:
	ans = 0
print(ans)


n = int(input())
a = input().split(' ')
streak = False
actions = 0
a.append('0')
for i in range(n + 1):
	if streak == True:
		if a[i] == '1':
			actions += 1
		else:
			streak = False
			actions += 1
	if streak == False:
		if a[i] == '1':
			streak = True
			actions += 1
if actions == 0:
	print(0)
else:
	print(actions - 1)


f = lambda : map(int, input().split())
n = int(input())
l = list(f())
c = 0
for i in range(1, n):
	if [l[i - 1], l[i]] != [0, 0]:
		c += 1
if l[0] == 1:
	c += 1
if l[-1] == 0:
	c -= 1
print(max(0, c))


n = int(input())
arr = list(map(int, input().split()))
c = arr.count(1)
if c == 0:
	print(0)
else:
	a = 0
	for i in range(n - 1):
		if arr[i] == 1 and arr[i + 1] == 1:
			a += 1
	print(c * 2 - 1 - a)


input()
ans = 0
is_list = True
letters = input().split()
for i in letters:
	if i == '1':
		ans += 1
		is_list = False
	elif not is_list:
		ans += 1
		is_list = True
print(max(ans - is_list, 0))


n = int(input())
letters = input()
letters = letters.split(' ')
cont = 0
lei_zero = 0
lei_uno = False
for x in letters:
	if x == '1':
		if lei_zero >= 1:
			cont += 1
		cont += 1
		lei_zero = 0
		lei_uno = True
	elif lei_uno:
		lei_zero += 1
print(cont)


def solve():
	n = int(input())
	A = list(map(int, input().split()))
	need = A.count(1)
	k = 0
	ans = 0
	i = 0
	j = 0
	while k != need:
		if j == 0:
			if A[i] == 1:
				A[i] = 0
				j = 1
				ans += 1
				k += 1
			else:
				i += 1
		elif A[i + 1] == 1:
			A[i + 1] = 0
			ans += 1
			k += 1
			i += 1
		else:
			j = 0
			ans += 1
	print(ans)
solve()


n = int(input())
ans = 0
op = False
for s in input().split():
	if s == '1' and op:
		ans += 1
	elif s == '0' and op:
		op = False
		ans += 1
	elif s == '1' and (not op):
		op = True
		ans += 1
print(ans - (not op and ans > 0))


n = int(input())
l = list(map(int, input().strip(' 0').split()))
if len(l) > 0:
	count = 0
	just_seen1 = False
	for c in l:
		if c == 1:
			count += 1
			just_seen1 = True
		elif just_seen1:
			count += 1
			just_seen1 = False
	print(count)
else:
	print(0)


n = int(input())
s = ''.join(input().split())
index = s.find('1')
if index < 0:
	operation = 0
else:
	operation = 1
	s = s[index + 1:]
while index + 1:
	index = s.find('1')
	if index == 0:
		operation += 1
	elif index > 0:
		operation += 2
	s = s[index + 1:]
print(operation)


n = int(input())
c = input().split()
if '1' not in c:
	print(0)
else:
	m = ''
	w = []
	flag = 0
	for i in range(len(c)):
		if c[i] == '1':
			m += '1'
			flag = 0
		else:
			if m != '':
				w.append(m)
			m = ''
			flag = 1
	if flag == 0:
		w.append(m)
	if len(w) == 1:
		print(len(w[0]))
	else:
		tot = 0
		for i in range(len(w) - 1):
			tot += len(w[i]) + 1
		tot += len(w[-1])
		print(tot)


n = int(input())
L = list(map(int, input().split(' ')))
s = []
for i in range(n):
	if L[i] == 1:
		s.append(i)
if len(s) != 0:
	count = 1
	for i in range(len(s) - 1):
		count = count + min(2, s[i + 1] - s[i])
	print(count)
else:
	print(0)


input()
s = input()
s = s.replace(' ', '')
s = s.replace('0', ' ')
s = s.strip()
s = '0'.join(s.split())
print(len(s))


n = int(input())
letters = [int(a) for a in input().split()]
unread = 0
changes = 0
last = 0
for letter in letters:
	if letter == 1:
		unread += 1
	if last != letter:
		last = letter
		changes += 1
if letters[0] == 0 and changes > 0:
	changes -= 1
if letters[n - 1] == 0 and changes > 0:
	changes -= 1
print(unread + changes // 2)


import sys
input = sys.stdin.readline
n = int(input())
l = input().split()
li = [int(i) for i in l]
l = []
for i in range(n):
	if li[i] == 1:
		l.append(i)
ans = 0
for i in range(len(l) - 1):
	ans += min(l[i + 1] - l[i], 2)
if l != []:
	ans += 1
print(ans)


num = int(input())
letters = [int(x) for x in input().split()]
count = 0
ones = letters.count(1)
for i in range(num):
	if letters[i] == 1:
		count += 1
		ones -= 1
		if i < num - 1 and letters[i + 1] == 0 and (ones > 0):
			count += 1
print(count)


n = int(input())
List = list(map(int, input().split()))
i = ans = h = 0
One = List.count(1)
while i < n:
	if List[i] == 1:
		ans += 1
		i += 1
		h += 1
		while i < n and List[i] == 1:
			i += 1
			ans += 1
			h += 1
		if One == h:
			break
		else:
			ans += 1
	i += 1
print(ans)


size = int(input())
emails = input().split(' ')
moves = 0
if size > 0:
	last_value = emails[0]
	if last_value == '1':
		moves += 1
for i in range(1, size):
	if emails[i] == '1':
		if last_value == '0' and moves != 0:
			moves += 1
		moves += 1
	last_value = emails[i]
print(moves)


n = int(input())
ps = input().split() + ['0']
qs = []
k = 0
for p in ps:
	if p == '1':
		k += 1
	elif p == '0':
		if k > 0:
			qs.append(k)
		k = 0
	else:
		print('WTF?!')
if qs:
	r = sum(qs) + len(qs) - 1
else:
	r = 0
print(r)


(n, a) = (int(input()), list(map(int, input().split())))
c = f = -1
for i in a:
	c = c + 1 if i == 1 and i == f else c + 2 if i == 1 else c + 0
	f = i
print(0 if c == -1 else c)


n = int(input())
s = list(map(int, input().split()))
f = []
i = 0
while i < n and s[i] != 1:
	i += 1
if i == n:
	print(0)
else:
	ans = 1
	while i < n:
		if s[i] == 1:
			l = 0
			while i < n - 1 and s[i + 1] == 0:
				l += 1
				i += 1
			if i < n - 1:
				f.append(l)
		i += 1
	i = 0
	while i < len(f):
		if f[i] == 0:
			ans += 1
		else:
			ans += 2
		i += 1
	print(ans)


n = int(input())
a = map(int, input().split())
r = 0
m = 0
for i in range(n):
	p = next(a)
	if p == 1:
		r += 1
		if m:
			r += 1
			m = 0
	elif r:
		m = 1
print(r)


n = int(input())
is_unread = list(map(int, input().split()))
operations = 0
i = 0
while i < n and (not is_unread[i]):
	i += 1
if i < n:
	operations += 1
while i < n:
	delta = 1
	while i + delta < n and (not is_unread[i + delta]):
		delta += 1
	if i + delta < n:
		if delta == 1:
			operations += 1
		else:
			operations += 2
	i += delta
print(operations)


n = int(input())
a = input().split()
res = sum((1 + (i + 1 == n or a[i + 1] == '0') for i in range(n) if a[i] == '1'))
print(res - 1 if res > 0 else res)


n = int(input())
ind = 0
indexs = []
for i in input().split():
	if i == '1':
		indexs.append(ind)
	ind += 1
if len(indexs) < 2:
	print(len(indexs))
	exit(0)
ans = 1
for i in range(1, len(indexs)):
	a = indexs[i - 1]
	b = indexs[i]
	if b - a <= 2:
		ans += b - a
	else:
		ans += 2
print(ans)


n = int(input())
s = input()
s = s.replace(' ', '')
s = s.replace('0', ' ')
s = s.strip()
print(len('0'.join(s.split())))


import sys
n = int(input())
lineas = sys.stdin.readlines()
can = 0
entro = 0
movimiento = 0
arr2 = []
can2 = 0
p = 0
alLado = 0
b = True
for linea in lineas:
	arr = linea.split()
	arr = list(map(int, arr))
	if len(arr) == 2:
		for i in range(len(arr)):
			if arr[i] == 1:
				movimiento += 1
				b = False
	if b:
		for i in range(n):
			if arr[i] == 0:
				can += 1
			elif arr[i] == 1 and can == 0:
				movimiento += 1
				entro = 1
			elif arr[i] == 1 and can == 1:
				if i >= 2:
					if arr[i - 2] == 1:
						movimiento += 2
						can = 0
						arr[i - 2] = 0
						entro = 1
				else:
					movimiento += 1
					can = 0
					entro = 1
			elif can >= 2 and arr[i] == 1:
				if entro == 0:
					movimiento += 1
					can = 0
					entro = 1
				else:
					movimiento += 2
					can = 0
print(movimiento)


n = int(input())
a = list(map(int, input().split()))
cnt = 1
k = 0
for i in range(n):
	st = i
	if a[i]:
		k = 1
		break
if not k:
	cnt = 0
for i in range(st + 1, n):
	if a[i]:
		if a[i - 1]:
			cnt += 1
		else:
			cnt += 2
print(cnt)


n = int(input())
l = list(map(int, input().split()))
r = []
for i in range(n):
	if l[i] == 1:
		r.append(i)
c = 0
if len(r) >= 1:
	c += 1
	for i in range(1, len(r)):
		if r[i] - r[i - 1] == 1:
			c += 1
		else:
			c += 2
print(c)


n = int(input())
a = [int(i) for i in input().split()]
t = 0
h = n - 1
while a[h] == 0:
	h -= 1
	if h < 0:
		break
if h == -1:
	print(0)
else:
	for i in range(h + 1):
		if a[i] == 1:
			t += 1
			if i != h and a[i + 1] == 0:
				t += 1
	print(t)


n = int(input())
emails = list(map(int, input().split()))
operations = 0
list_view = True
for (i, email) in enumerate(emails):
	if list_view:
		if email:
			operations += 1
			list_view = False
	elif email:
		if emails[i - 1]:
			operations += 1
		else:
			operations += 2
print(operations)


n = int(input())
l = list(map(int, input().split()))
for i in range(1, n - 1):
	if l[i - 1:i + 2] == [1, 0, 1]:
		l[i] = 1
secv = 0
if l[0] == 1:
	secv += 1
for i in range(n):
	if l[i:i + 2] == [0, 1]:
		secv += 1
if secv > 0:
	secv -= 1
print(l.count(1) + secv)


import sys
n = int(input())
letters = list(map(int, sys.stdin.readline().split()))

def getNbOps(l):
	if len(l) == 0:
		return 0
	elif l[0] == 0:
		try:
			idx = l.index(1)
			return getNbOps(l[idx:])
		except:
			return 0
	else:
		try:
			idx = l.index(0)
			return idx + 1 + getNbOps(l[idx:])
		except:
			return len(l) + 1
print(max(0, getNbOps(letters) - 1))


n = int(input())
S = list(map(int, input().split()))
cur = 0
for (i, q) in enumerate(S):
	if S[i] == 1 and i > 0 and (S[i - 1] == 1):
		cur += 1
	elif S[i] == 1:
		if cur == 0:
			cur += 1
		else:
			cur += 2
print(cur)


n = int(input())
emails = list(map(int, input().split()))
ans = 0
for e in range(len(emails)):
	if emails[e] == 1:
		ans += 1
	if e + 1 < len(emails) and emails[e] == 1 and (emails[e + 1] != 1):
		ans += 1
if emails[n - 1] != 1 and ans != 0:
	ans -= 1
print(ans)


k = int(input())
l = list(map(int, input().split()))
(ans, i, bool) = (0, 0, True)
while i < k:
	if l[i] == 1:
		bool = False
		ans += 1
		for num in l[i + 1:]:
			i += 1
			if num == 1:
				ans += 1
			else:
				ans += 1
				break
	i += 1
print(0 if bool else ans if l[-1] == 1 else ans - 1)


n = int(input())
line = str(input()).replace(' ', '').split('0')
num = 0
for it in line:
	if len(it) > 0:
		num += len(it) + 1
if num > 0:
	print(num - 1)
else:
	print(num)


n = int(input())
arr = list(map(int, input().split()))
arr.append(0)
op = 0
i = 0
lastzero = False
while True:
	if arr[i] == 1:
		op += 1
		if arr[i + 1] == 0:
			op += 1
	if i + 1 != len(arr):
		i += 1
	else:
		break
if op < 1:
	print(0)
else:
	print(op - 1)


input()
lis = list(map(int, input().split()))
for i in range(1, len(lis)):
	if lis[len(lis) - 1] == 0:
		lis.pop()
count = 0
i = 0
while i < len(lis):
	if lis[i] == 1:
		count += 1
		if i + 1 == len(lis):
			break
		elif lis[i + 1] == 0:
			count += 1
			i += 1
	i += 1
print(count)


n = int(input())
letters = list(map(int, input().split(' ')))
for i in reversed(list(range(n))):
	if all((x == 0 for x in letters[-(i + 1):])):
		letters = letters[:-(i + 1)]
count = 0
prev = -1
for x in letters:
	if x == 1 or prev == 1:
		count += 1
	prev = x
print(count)


n = int(input())
a = input().split()
print(max(0, sum((1 + (i + 1 == n or a[i + 1] == '0') for i in range(n) if a[i] == '1')) - 1))


input()
print(len('a'.join(input().replace(' ', '').replace('0', ' ').split())))


n = int(input())
lst = list(map(int, input().split()))
i = 0
count = 0
while i < n - 1:
	if lst[i] == 1:
		count += 1
		if lst[i + 1] == 0:
			count += 1
	i += 1
if lst[n - 1] == 0:
	count = max(0, count - 1)
else:
	count += 1
print(count)


n = int(input())
string = input()
numbers = list(map(int, string.split()))
b = 0
if 1 in numbers:
	a = n - 1 - numbers[::-1].index(1)
	streak = 0
	for x in range(a + 1):
		if numbers[x] == 1:
			streak += 1
			b += 1
		elif streak != 0:
			streak = 0
			b += 1
print(b)


a = int(input())
b = input().split()
l = []
for i in range(a):
	if b[i] == '1':
		l.append(i)
if len(l) != 0:
	s = 1
	for i in range(1, len(l)):
		if l[i] - l[i - 1] == 1:
			s = s + 1
		else:
			s = s + 2
	print(s)
else:
	print(0)


count = int(input())
array = [int(i) for i in input().split()]
dic = dict()
lol = 0
click = 0
for i in range(count):
	if array[i] == 1:
		dic[i] = 1
for i in dic:
	lol = i
	break
for i in dic:
	if i - lol >= 2:
		click += 2
		lol = i
	else:
		click += 1
		lol = i
print(click)


from itertools import groupby
n = int(input())
a = map(int, input().split())
c = 0
for (r, g) in groupby(a):
	if r == 1:
		c += len(list(g)) + 1
print(max(0, c - 1))


n = int(input())
total = 0
for _ in ''.join(input().split()).split('0'):
	if _ != '':
		total += len(_)
		total += 1
print(total - 1 if total != 0 else 0)


n = int(input())
str = ''.join(input().split(' '))
prev = -1
count = 0
for i in range(0, n):
	if str[i] == '1':
		if i - prev <= 2 and prev != -1:
			count += i - prev
		elif prev == -1:
			count += 1
		else:
			count += 2
		prev = i
print(count)


def main():
	n = input()
	letters = input().split()
	lst = -1
	cnt = 0
	for (i, x) in enumerate(letters):
		if x == '1':
			if lst + 1 == i or lst == -1:
				cnt += 1
			else:
				cnt += 2
			lst = i
	print(max(0, cnt))
main()


import sys
letters = map(int, sys.stdin.readline().rstrip())
total = 0
inInbox = True
letters = list(map(str, sys.stdin.readline().rstrip().split(' ')))
for l in letters:
	if l == '1':
		total += 1
		inInbox = False
	elif l == '0' and inInbox == False:
		inInbox = True
		total += 1
if letters[-1] == '0':
	total -= 1
print(max(0, total))


input()
a = input().split()
b = []
c = 0
for i in a:
	if i == '1':
		c += 1
	else:
		if c:
			b += [c]
		c = 0
if c:
	b += [c]
print(max(sum(b) + len(b) - 1, 0))


input()
X = list(map(int, input().split()))
if 1 not in X:
	print(0)
	exit()
(i, Moves) = (X.index(1), 0)
while i < len(X):
	if X[i] == 1:
		Moves += 1
		while i + 1 < len(X) and X[i + 1] == 1:
			i += 1
			Moves += 1
	i += 1
	Moves += 1 if i - 1 < len(X) and X[i - 1] == 1 else 0
print(Moves - 1)


n = int(input())
letters = input()
letters = letters.replace(' ', '')
letters = letters.strip('0')
letters = [int(x) for x in letters]
steps = 0
unread = []
for x in range(len(letters)):
	if letters[x] == 1:
		unread.append(letters[x])
	else:
		if letters[x] == 0 and letters[x - 1] == 0:
			continue
		if letters[x] == 0:
			steps += 1
print(steps + len(unread))


n = int(input())
z = list(map(int, input().split()))
last = -1
se = [[z[0], 1]]
for i in z[1:]:
	if i != se[-1][0]:
		se.append([i, 0])
	se[-1][1] += 1
tot = 0
flag = False
for i in se:
	if i[0] == 1:
		flag = True
		tot += i[1] + 1
print(tot - flag)


a = int(input())
List = map(int, input().split())
List = list(List)
i = 0
resp = 0
while i < a:
	if List[i] == 1:
		resp += 1
		if i == a - 1:
			resp += 1
			break
		elif List[i + 1] != 1:
			resp += 1
	i += 1
if resp > 0:
	print(resp - 1)
else:
	print(resp)


input()
a = input()
if a.count('1') == 0:
	print(0)
	exit()
while '0 0' in a:
	a = a.replace('0 0', '0')
a = a.split()
print(len(a[a.index('1'):len(a) - list(reversed(a)).index('1')]))


n = int(input())
let = list(map(int, input().split()))
inbox = 1
ans = 0
for l in let:
	if inbox == 1:
		if l == 1:
			ans += 1
			inbox = 0
	elif l == 1:
		ans += 1
	else:
		ans += 1
		inbox = 1
if ans > 0 and let[n - 1] == 0:
	ans -= 1
print(ans)


input()
a = input() + ' '
print(a.count('1') + max(len([*filter(None, a.split('0 '))]) - 1, 0))


n = int(input())
z = list(map(int, input().split()))
count = 0
for i in range(n):
	if z[i] == 1:
		count += 1
		if i + 1 != n:
			if z[i + 1] == 0:
				count += 1
			else:
				counti = 1
				if counti + i < n:
					while z[counti + i] == 1:
						counti += 1
						count += 1
						z[counti + i - 1] = 0
						if counti + i >= n:
							break
					count += 1
		else:
			count += 1
if count == 0:
	print(0)
else:
	print(count - 1)


n = int(input())
states = [int(c) for c in input().split()]
while states and states[-1] == 0:
	states.pop()
if not states:
	print(0)
	exit()
i = 0
while states[i] == 0:
	i += 1
ans = 0
while i < len(states):
	if states[i] == 1:
		ans += 1
		i += 1
	elif states[i] == 0:
		ans += 1
		i += 1
		while i < n and states[i] == 0:
			i += 1
print(ans)


def get_ans():
	n = int(input())
	l = list(map(int, input().split()))
	if 1 not in l:
		return 0
	if 0 not in l:
		return len(l)
	moves = 0
	idx = -1
	ll = len(l)
	while idx < ll - 1:
		if l[idx + 1] == 1:
			moves += 1
			idx += 1
		elif idx >= 0:
			if l[idx] == 0:
				idx += 1
				continue
			else:
				idx += 1
				moves += 1
		else:
			idx += 1
	if l[-1] == 1:
		return moves
	else:
		return moves - 1
ans = get_ans()
print(ans)


n = int(input())
lista = list(map(int, input().split()))
unsJuntos = 0
uns = 0
for i in range(len(lista)):
	if lista[i] == 1:
		uns += 1
	if i + 1 < len(lista) and lista[i] == 1 and (lista[i + 1] == 1):
		unsJuntos += 1
resposta = uns * 2 - 1 - unsJuntos
if uns == 0:
	resposta = 0
print(resposta)


n = int(input())
lista = list(map(int, input().split()))
ans = 0
for i in range(len(lista)):
	if lista[i] == 1:
		ans += 1
	if i + 1 < len(lista) and lista[i] == 1 and (lista[i + 1] != 1):
		ans += 1
if lista[n - 1] != 1 and ans != 0:
	ans -= 1
print(ans)


n = int(input())
l_r = input().split()
t = -1
p_r = False
for r in l_r:
	if r == '1':
		t += 1
		if not p_r:
			t += 1
		p_r = True
	else:
		p_r = False
print(t if t != -1 else 0)


input()
s = ''.join(input().split())
clicks = s.count('1') + len(s.replace('0', ' ').split()) - 1
print(max(0, clicks))


n = int(input())
prev = u = g = 0
for i in (int(x) for x in input().split()):
	if i == 1:
		u += 1
		if prev == 0:
			g += 1
	prev = i
print(u + g - 1 if u > 0 else 0)


n = int(input())
a = map(int, input().split())
p = 0
res = -1
for i in a:
	if i == p == 1:
		res += 1
	if i == 1 and p == 0:
		res += 2
	p = i
print(max(0, res))


n = int(input())
z = list(map(int, input().split()))
if 1 not in z:
	print(0)
	exit()
counter = 1
tmp = z.count(1) - 1
i = z.index(1)
while tmp > 0:
	j = i
	i = z[i + 1:].index(1) + i + 1
	if i - 1 == j:
		counter += 1
		tmp -= 1
	else:
		counter += 2
		tmp -= 1
print(counter)


from itertools import *
input()
print(max(0, sum((len(list(g)) + 1 for (r, g) in groupby(map(int, input().split())) if r == 1)) - 1))


n = int(input())
s = list(map(int, input().split()))
ids = []
for i in range(n):
	if s[i] == 1:
		ids.append(i)
x = len(ids)
c = 1
if len(ids) == 0:
	print(0)
else:
	for i in range(x - 1):
		diff = ids[i + 1] - ids[i]
		if diff > 2:
			c += 2
		else:
			c += diff
	print(c)


x = input()
numbers = input().split(' ')
for i in range(0, len(numbers)):
	numbers[i] = int(numbers[i])
m = 0
for l in range(len(numbers)):
	if numbers[l] == 1:
		m += 2
		numbers[l] = 0
		if l < len(numbers) - 1:
			if numbers[l + 1] == 1:
				m -= 1
if m > 1:
	m = m - 1
print(m)


n = int(input())
f = [int(x) for x in input().split()]
c = 0
gone_out = False
k = 0
for i in f:
	if i == 1 and k == 0:
		c += 1
		gone_out = False
	elif i == 1 and k == 1:
		c += 1
	elif i == 0 and k == 1:
		gone_out = True
		c += 1
	k = i
if gone_out:
	c -= 1
print(c)


n = int(input())
letters = input().split(' ')
operations = 0
curr = -1
for i in range(n):
	if curr == -1 and letters[i] == '1':
		curr = i
		operations += 1
	elif curr >= 0 and letters[i] == '1':
		if i - curr > 2:
			operations += 2
			curr = i
		else:
			operations += i - curr
			curr = i
print(operations)


n = int(input())
a = list(map(int, input().split(' ')))
res = 0
i = 0
while i < len(a):
	if a[i] == 1:
		res += 1
		while i < len(a) and a[i] == 1:
			i += 1
			res += 1
	i += 1
print(max(0, res - 1))


n = int(input())
a = list(map(int, input().split()))
cnt = 0
last = -1
for i in range(len(a)):
	if a[i] == 1:
		if last == -1:
			cnt += 1
			last = i
		else:
			cnt += min(2, i - last)
			last = i
print(cnt)


import math
from collections import defaultdict

def input_ints():
	return list(map(int, input().split()))

def solve():
	n = int(input())
	a = ''.join(input().split()).split('0')
	ans = 0
	for x in a:
		if x:
			ans += len(x) + 1
	if ans:
		ans -= 1
	print(ans)
solve()


letras = int(input())
linha = input().split()
certo = 0
cont = 0
for i in range(len(linha)):
	if linha[i] == '1':
		certo += 1
cont2 = 0
for i in range(len(linha)):
	if linha[i] == '1':
		cont += 1
		cont2 += 1
		if i < len(linha) - 1:
			if linha[i + 1] == '0' and cont2 < certo:
				cont += 1
print(cont)


n = int(input())
state = 0
operations = 0
messages = input().split()
for m in messages:
	m = int(m)
	if m and state == 0:
		operations += 1
		state = 1
	elif m and state == 2:
		operations += 2
		state = 1
	elif m and state == 1:
		operations += 1
	elif not m and state == 1:
		state = 2
print(operations)


def main():
	input()
	l = [i for (i, _) in enumerate(map(int, input().split())) if _]
	if len(l) > 1:
		print(sum((min(i - j, 2) for (i, j) in zip(l[1:], l))) + 1)
	else:
		print(len(l))
main()


def reading_mail(n, mail):
	total_op = 0
	index = 0
	prev = 0
	while index < n:
		if 1 in mail[index:]:
			if mail[index] == 0 and prev == 1:
				total_op += 1
				prev = 0
			elif mail[index] == 1 and prev == 1:
				total_op += 1
			elif mail[index] == 1 and prev == 0:
				total_op += 1
				prev = 1
			index += 1
		else:
			break
	return total_op
n = int(input())
mail = list(map(int, input().split()))
print(reading_mail(n, mail))


import os
import re
import sys
from bisect import bisect, bisect_left, insort, insort_left
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from io import BytesIO, IOBase
from itertools import accumulate, combinations, combinations_with_replacement, groupby, permutations, product
from math import acos, asin, atan, ceil, cos, degrees, factorial, hypot, log2, pi, radians, sin, sqrt, tan
from operator import itemgetter, mul
from string import ascii_lowercase, ascii_uppercase, digits

def inp():
	return int(input())

def inlist():
	return list(map(int, input().split()))

def instr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def invrf():
	return map(float, input().split())

def main():
	n = inp()
	a = inlist()
	c = []
	d = []
	for i in a:
		if i == 0:
			if len(d) > 0:
				c.append(d)
			d = []
		else:
			d.append(i)
	if len(d) > 0:
		c.append(d)
	res = max(len(c) - 1, 0) + sum(a)
	print(res)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
main()


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	if 1 not in arr:
		print(0)
		exit(0)
	idx = arr.index(1)
	result = 0
	reading = True
	while idx < n:
		if arr[idx] == 1:
			result += 1
			if not reading:
				result += 1
			reading = True
		else:
			reading = False
		idx += 1
	print(result)
main()


z = int(input())
ans = 0
x = input().split()
if '1' not in x:
	print(0)
else:
	for i in range(x.index('1') + 1, z):
		if x[i] == '1':
			if x[i - 1] == '1':
				ans += 1
			else:
				ans += 2
	print(ans + 1)


def nextUnread(a, i, n):
	for p in range(i + 1, n):
		if a[p] == 1:
			return p
	return -1
n = int(input())
a = list(map(int, input().split()))
if 1 not in a:
	print(0)
else:
	c = 1
	i = nextUnread(a, -1, n)
	while i < n - 1:
		x = nextUnread(a, i, n)
		if x == -1:
			break
		if x - i == 1:
			c += 1
		else:
			c += 2
		i = x
	print(c)


n = int(input())
a = [int(k) for k in input().split()]
t = -1
pri = 0
for i in a:
	if pri == i == 1:
		t += 1
	if pri == 0 and i == 1:
		t += 2
	pri = i
print(max(0, t))


import sys
import math
n = int(sys.stdin.readline())
instr = sys.stdin.readline().split()
cc = 0
result = 0
ki = 0
for i in range(n):
	if instr[i] == '1':
		ki = i
		break
for i in range(ki, n):
	if instr[i] == '1':
		if cc >= 1:
			result += 1
		cc = 0
		result += 1
	else:
		cc += 1
print(result)


n = int(input().strip())
a = list(map(int, input().split()))
pitem = 0
count = 0
for item in a:
	if pitem != 0 or item != 0:
		count += 1
	pitem = item
if a[-1] == 0:
	count -= 1
print(max(0, count))


n = int(input())
a = list(map(int, input().split()))
k = 0
flag = False
for i in range(n):
	if a[i] == 1 and flag == False:
		k += 1
		if i + 1 < n:
			if a[i + 1] == 0:
				flag = True
	if a[i] == 0 and flag == True:
		k += 1
		flag = False
if a[-1] == 0:
	k -= 1
if k < 0:
	print(0)
else:
	print(k)


n = int(input())
a = list(map(int, input().split()))
(res, ans, temp) = (0, 0, 0)
for i in a:
	if i == 1:
		ans += 1
		temp = 1
	elif i == 0 and temp == 1:
		ans += 1
		temp = 0
if temp == 0:
	print(max(0, ans - 1))
else:
	print(ans)


from sys import stdin, stdout
input = stdin.readline
n = int(input())
a = input().split()
p = True
ans = 0
for i in range(n):
	if a[i] == '1':
		if p:
			ans += 1
		else:
			ans += 2
			p = True
	elif ans != 0:
		p = False
stdout.write(str(ans))


letters = list()
lettersqnt = input()
letterstring = input()
lettersqnt = int(lettersqnt)
flagjmp = 0
numberofactions = 0
distancebetw = 0
letters = letterstring.split()
for i in range(0, lettersqnt):
	if letters[i] == '1' and flagjmp == 0:
		flagjmp = 1
		numberofactions = numberofactions + 1
	elif letters[i] == '1' and flagjmp == 1:
		if distancebetw == 0:
			numberofactions = numberofactions + 1
		else:
			distancebetw = 0
			numberofactions = numberofactions + 2
	elif flagjmp == 1:
		distancebetw = distancebetw + 1
print(numberofactions)


n = int(input())
a = list(map(int, input().split()))
ans = 0
if a.count(1) > 0:
	b = a.index(1)
	ans += 1
	for i in range(b + 1, n):
		if a[i] == 1 and a[i - 1] == 1:
			ans += 1
		elif a[i] == 1 and a[i - 1] == 0:
			ans += 2
print(ans)


n = int(input())
l = input().split()
(s, i) = (0, 0)
while i < n:
	if l[i] == '1':
		while i < n and l[i] == '1':
			i += 1
			s += 1
		s += 1
	i += 1
if s == 0:
	print(0)
else:
	print(s - 1)


n = int(input())
a = list(map(int, input().split()))
s = 0
pred = 0
for i in range(n):
	if a[i] == 1:
		s += 1
	elif pred == 1:
		s += 1
	pred = a[i]
if a[-1] == 0:
	s -= 1
if s < 0:
	s = 0
print(s)


n = int(input())
l = list(map(int, input().split()))
v = 1
k = 0
r = 0
for i in range(n):
	if l[i] == 1:
		if v == 1:
			k = k + 1
			v = 0
			r = 0
		elif v == 0 and r == 0:
			k = k + 1
			r = 0
			v = 0
		else:
			k = k + 2
			r = 0
			v = 0
	else:
		r = r + 1
print(k)


n = int(input())
s = input()
a = s.split()
b = []
x = 0
for i in range(len(a)):
	if a[i] == '1':
		x += 1
	elif x != 0:
		b.append(x)
		x = 0
if x != 0:
	b.append(x)
if len(b) != 0:
	print(sum(b) + len(b) - 1)
else:
	print('0')


amountEmails = int(input())
listEmails = input().split()
operator = 0
amtOnes = 0
for i in range(len(listEmails)):
	if listEmails[i] == '1':
		amtOnes += 1
	listEmails[i] = int(listEmails[i])
index = 0
while index < amountEmails:
	if listEmails[index] == 1:
		operator += 1
		amtOnes -= 1
		if index + 1 < amountEmails and listEmails[index + 1] == 0 and (amtOnes > 0):
			operator += 1
			index += 1
	index += 1
print(operator)


n = int(input())
mail_list = list(map(int, input().split()))
counter = 0
i = 0
while i < len(mail_list) - 1:
	if mail_list[i] and mail_list[i + 1]:
		counter += 1
	elif mail_list[i]:
		counter += 2
	i += 1
if mail_list[-1]:
	counter += 1
else:
	counter -= 1
print(counter if counter > 0 else 0)


n = input()
n = input().split()
d = 0
i = 0
one = 0
while i < len(n):
	if n[i] == '1' and one == 0:
		d += 1
		one = 1
	elif (n[i] == '1' and n[i - 1] == '1' and (i >= 1)) and one == 1:
		d += 1
	elif n[i] == '1' and n[i - 1] != '1' and (i >= 2) and (one == 1):
		d += 2
	i += 1
print(d)


def ans(a, l, b):
	m = 0
	f = 0
	k = len(l)
	m = l[0]
	f = l[1]
	x = l[k - 1]
	y = l[k - 2]
	sum = max((x + y) % b, (m + f) % b)
	return sum
k = -2
f = 0
t = int(input())
l = list(map(int, input().split()))
for i in range(0, t, 1):
	if l[i]:
		if k == i - 1:
			f += 1
		elif k == -2:
			f += 1
		else:
			f += 2
		k = i
if f == 0:
	print(0)
else:
	print(f)


n = int(input())
x = list(map(int, input().split()))
otv = 0
for i in range(n):
	if x[i] == 1:
		otv += 1
		if i < n - 1 and x[i + 1] != 1:
			otv += 1
if x[i] != 1:
	print(max(otv - 1, 0))
else:
	print(otv)


n = int(input())
a = input()
beg = a.find('1')
ending = a.rfind('1')
sub = a[beg:ending + 1]
if not sub:
	print(0)
else:
	s = [i for i in sub.split('0') if not i.isspace()]
	t = len(s)
	p = 0
	for i in s:
		for j in i:
			if j == '1':
				p += 1
	print(p + t - 1)


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


def rint():
	return int(input())

def rints():
	return list(map(int, input().split()))
from itertools import groupby
n = rint()
s = rints()
ops = 0
for (k, g) in groupby(s):
	if k == 1:
		c = sum((1 for _ in g))
		ops += c + 1
print(max(0, ops - 1))


import sys
import math
import itertools
import collections

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = prime[1] = False
	r = [p for p in range(n + 1) if prime[p]]
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ''.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base=3):
	newnumber = ''
	while number > 0:
		newnumber = str(number % base) + newnumber
		number //= base
	return newnumber
n = ii()
a = li()
if 1 not in a:
	print(0)
else:
	ans = 0
	for i in range(n):
		s = wr(a)
		ans += s.count('1' * (n - i)) * (n - i + 1)
		s = s.replace('1' * (n - i), '0' * (n - i))
		a = list(map(int, s))
	print(ans - 1)


n = int(input())
a = [int(x) for x in input().split()]
(s, p, flag) = (0, 0, False)
for i in range(n):
	if a[i] == 1 and p < 1:
		s += 1
		(p, flag) = (0, True)
	elif a[i] == 1:
		s += 2
		(p, flag) = (0, True)
	elif flag:
		p += 1
print(s)


n = int(input())
letters = [int(x) for x in input().split()]
count = 0
res = 0
i = 0
while i < n:
	if letters[i] == 1:
		break
	i += 1
while i < n:
	if letters[i] == 1:
		if count == 0:
			res += 1
		else:
			res += 2
		count = 0
	else:
		count += 1
	i += 1
print(res)


input()
lst = [i for i in ''.join(input().split()).split('0') if i]
if not lst:
	print(0)
else:
	print(len(lst) - 1 + sum([len(i) for i in lst]))


import sys, re
print(len(re.sub('0+', '0', sys.stdin.readlines()[1][:-1].replace(' ', '')).strip('0')))


n = int(input())
arr = list(map(int, input().split()))
closed = True
ris = 0
for x in range(len(arr)):
	if arr[x] == 0 and (not closed) and (1 in arr[x:]):
		closed = True
		ris += 1
	elif arr[x] == 1 and closed:
		ris += 1
		closed = False
	elif arr[x] == 1 and (not closed):
		ris += 1
print(ris)


n = int(input())
ls = [int(u) for u in input().split()]
ones = ls.count(1)
val = 0
final = 0
count = 0
for u in ls:
	if u == 1:
		count = 1
	elif count == 1 and u == 0:
		final += 1
		count = 0
if count == 1:
	final += 1
print(max(0, ones + final - 1))


n = input()
str_input = input()
arr = [int(val) for val in str_input.split(' ')]
sum = 0
last = -1
for (idx, val) in enumerate(arr):
	if val == 1:
		if last != -1:
			diff = idx - last
			if diff <= 2:
				sum += diff
			else:
				sum += 2
		else:
			sum += 1
		last = idx
print(sum)


n = int(input())
letters = input().split()
letters = ''.join(letters)
letters = letters.split('0')
unread_list = []
for element in letters:
	if element:
		unread_list.append(element)
if unread_list:
	operations = 0
	for i in range(len(unread_list) - 1):
		operations += 1 + len(unread_list[i])
	operations += len(unread_list[-1])
	print(operations)
else:
	print(0)


import itertools
import bisect
import math
from collections import *
import os
import sys
from io import BytesIO, IOBase
ii = lambda : int(input())
lmii = lambda : list(map(int, input().split()))
slmii = lambda : sorted(map(int, input().split()))
li = lambda : list(input())
mii = lambda : map(int, input().split())
msi = lambda : map(str, input().split())

def main():
	n = ii()
	a = lmii()
	cnt = 0
	s = 0
	f = 0
	for i in range(n):
		if a[i] == 1:
			if f == 0:
				cnt += 1
				f = 1
			s += 1
		else:
			f = 0
	print(max(0, s + cnt - 1))
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


n = int(input())
a = list(map(int, input().split()))
k = 0
for i in range(n - 1):
	if a[i] == 1:
		k += 2
		if a[i + 1] == 1:
			k -= 1
if a[-1] == 1:
	k += 1
if a[-1] != 1 and k > 0:
	k -= 1
print(k)


def main():
	n_mails = int(input())
	mails = [int(_) for _ in input().split()]
	opening_times = sum(mails)
	switching_times = sum((mails[i] == 1 and mails[i - 1] == 0 for i in range(1, n_mails)))
	if switching_times > 0 and mails[0] == 0:
		switching_times -= 1
	print(opening_times + switching_times)
main()


n = int(input())
a = list(map(int, input().split()))
a.insert(0, 0)
a.append(0)
i = 1
q = a.count(1)
sum = 0
while i != len(a) - 1:
	if a[i] == 1:
		sum += 1
		q -= 1
		while a[i + 1] == 1:
			i += 1
			sum += 1
			q -= 1
		if i != len(a) - 2 and q > 0:
			sum += 1
	i += 1
print(sum)


num_letters = input()
letter_box = input().split(' ')

def read_letters(letters):
	current = 0
	operations = 0
	on_list = False
	while current < int(num_letters):
		if letter_box[current] == '1' and on_list == False:
			operations += 1
			on_list = True
		elif letter_box[current] == '0' and on_list == True:
			if current + 1 < int(num_letters):
				if letter_box[current + 1] == '1':
					operations += 1
					on_list = False
		elif letter_box[current] == '1' and on_list == True:
			operations += 1
		current += 1
	print(operations)
read_letters(letter_box)


n = int(input())
l = list(map(int, input().split()))
t = 0
for i in range(n):
	if l[i] == 1:
		t = t + 1
		if i < n - 1 and l[i + 1] != 1:
			t = t + 1
if l[i] != 1:
	print(max(t - 1, 0))
else:
	print(t)


input()
t = input()[::2]
(k, a) = (0, '0')
for b in t[t.find('1'):t.rfind('1') + 1]:
	if b == '0':
		if a == '1':
			k += 1
	else:
		k += 1
	a = b
print(k)


t = int(input())
entrada = input().split()
for i in range(t):
	entrada[i] = int(entrada[i])
total = 0
k = 0
while k < t:
	if entrada[k] == 1:
		total += 1
	else:
		k += 1
		continue
	if k + 1 == t:
		break
	if entrada[k + 1] == 1:
		k += 1
	elif k + 2 == t:
		k += 2
	else:
		k += 2
		total += 1
if entrada[t - 1] == 0 and entrada[t - 2] == 0 and (total > 0):
	total -= 1
print(total)


n = input()
a = [*map(int, input().split())]
x = a.count(1)
y = 0
pre = 0
for i in a:
	if i == 1 and pre == 0:
		y += 1
	pre = i
print(max(0, x + y - 1))


input()
print(max(0, sum((len(i) + 1 for i in input().replace(' ', '').replace('0', ' ').split())) - 1))


n = int(input())
a = list(map(int, input().split(' ')))
flag = False
for i in range(n):
	if a[i] == 0:
		flag = False
		continue
	if not flag:
		flag = True
		a[i] += 1
	else:
		pass
s = sum(a)
if s == 0:
	print(s)
else:
	print(s - 1)


n = int(input())
x = list(map(int, input().split(' ')))
if 1 not in x:
	print(0)
	exit()
(count, i) = (0, x.index(1))
while i < n:
	if x[i] == 1:
		count += 1
		while i + 1 < n and x[i + 1] == 1:
			count += 1
			i += 1
	i += 1
	count += 1 if x[i - 1] == 1 else 0
print(count - 1)


n = int(input())
necessarias = 0
anterior = -1
string = ''.join(input().split(' '))
for email in range(0, n):
	if string[email] == '1':
		if email - anterior <= 2 and anterior != -1:
			necessarias += email - anterior
		elif anterior == -1:
			necessarias += 1
		else:
			necessarias += 2
		anterior = email
print(necessarias)


def pochta(lst):
	if all((x == lst[0] == 0 for x in lst)):
		return 0
	(k1, k2) = (0, 0)
	for i in range(len(lst)):
		if lst[i] == 1:
			k1 = i
	(flag1, flag2) = (1, 0)
	for i in range(0, k1 + 1):
		if lst[i] == 1:
			k2 += lst[i]
			(flag2, flag1) = (1, 0)
		elif lst[i] == 0 and flag2 and (not flag1):
			k2 += 1
			flag1 = 1
	return k2
n = int(input())
a = [int(i) for i in input().split()]
print(pochta(a))


number_of_letters = int(input())
operations = 0
letters = [int(x) for x in input().split()]
for i in range(len(letters)):
	if number_of_letters == len(letters):
		if letters[i] == 1:
			operations += 1
			if letters[i] > 0 and operations > 1 and (letters[i - 1] == 0):
				operations += 1
print(operations)


n = int(input())
t = [i for i in input().split()]
k = False
c = 0
for i in t:
	if i == '1' and (not k):
		k = True
		c += 1
	if k:
		if i == '0':
			k = False
		else:
			c += 1
print(c - 1 * (c != 0))


n = int(input())
l = 0
op = 0
a = list(map(int, input().split()))
for i in range(len(a) - 1):
	if a[i] == 1 and a[i + 1] == 1:
		op += 1
	elif a[i] == 1 and a[i + 1] == 0:
		op += 1
		if a[i + 1:].count(1) > 0:
			l += 1
if n == 1:
	print(a.count(1))
else:
	if a[-2] == 1 and a[-1] == 1:
		op += 1
	elif a[-1] == 1 and a[-2] == 0:
		l += 1
	print(op + l)


n = int(input())
r = input().split()
o = 0
a = 3
for i in range(n):
	if r[i] == '1':
		o += 1
		if a == 1:
			o += 1
		a = 0
	elif a == 0:
		a = 1
print(o)


n = int(input())
arr = list(map(int, input().split()))
l = -1
ans = 0
for (j, aj) in enumerate(arr):
	if aj == 0:
		continue
	elif l == -1:
		ans += 1
		l = j
	else:
		ans += min(j - l, 2)
		l = j
print(ans)


n = int(input())
cur = ans = 0
for i in input().split():
	if i == '1':
		cur += 1
	else:
		ans += (cur + 1) * (cur != 0)
		cur = 0
ans += (cur + 1) * (cur != 0)
print(max(0, ans - 1))


n = int(input())
l = list(map(int, input().split()))
while len(l) > 0 and l[-1] == 0:
	l.pop()
res = 0
f = False
prev = 0
for i in range(len(l)):
	if l[i] == 1:
		res += 1
		f = True
	elif l[i] == 0 and prev == 0 and (f == True):
		res += 1
		f = False
	elif l[i] == 0 and l[i + 1] == 1 and (f == True):
		res += 1
	prev = l[i]
print(res)


ncartas = int(input())
geral = ''.join(input().split(' '))
anterior = -1
necessarias = 0
for carta in range(0, ncartas):
	if geral[carta] == '1':
		if carta - anterior <= 2 and anterior != -1:
			necessarias += carta - anterior
		elif anterior == -1:
			necessarias += 1
		else:
			necessarias += 2
		anterior = carta
print(necessarias)


size = int(input())
emails = list(map(int, input().split(' ')))
count = 0
for i in range(size):
	if emails[i] == 1:
		count += 1
	elif emails[i - 1] == 1 and i != 0:
		count += 1
if emails[-1] == 0 and count > 0:
	count -= 1
print(count)


n = int(input())
arr = list(map(int, input().split()))
temp = []
for i in range(n):
	if arr[i] == 1:
		temp.append(i)
if len(temp) == 0:
	print(0)
elif len(temp) == 1:
	print(1)
else:
	ans = 1
	for i in range(len(temp) - 1):
		if temp[i + 1] - temp[i] == 1:
			ans += 1
		else:
			ans += 2
	print(ans)


m = int(input())
l = list(map(int, input().split()))
p = 0
f = False
for i in range(m):
	if l[i] == 1 and f == False:
		p = p + 1
		if i + 1 < m:
			if l[i + 1] == 0:
				f = True
	if l[i] == 0 and f == True:
		p = p + 1
		f = False
if l[m - 1] == 0 and p > 0:
	p = p - 1
print(p)


import sys
size = int(input())
array = list(map(int, input().split()))
if 1 not in array:
	print('0')
	sys.exit()
counter = 0
while 1 in array:
	buffer = array.index(1)
	i = buffer
	while i < size and array[i] == 1:
		counter += 1
		array[i] = 0
		i += 1
	if 1 in array:
		counter += 1
print(counter)


import sys
n = int(input())
a = list(map(str, input().split()))
s = str()
for i in a:
	s += i
s = s.strip('0')
if len(s) == 0:
	print(0)
	sys.exit(0)
res = 1
for i in range(1, len(s)):
	if s[i] == '1' and s[i - 1] != '1':
		res += 2
	elif s[i] == '1':
		res += 1
print(res)


a = int(input())
listx = list(map(int, input().split(' ')))
listx2 = [int(x) for x in range(a) if listx[x] == 1]
listx3 = [listx2[x] - listx2[x - 1] - 1 for x in range(1, len(listx2))]
if listx2 == []:
	print(0)
else:
	total = 1
	for i in listx3:
		if i == 0:
			total += 1
		else:
			total += 2
	print(total)


n = input()
l = input().split()
x = 0
for i in range(len(l)):
	if l[i] == '1':
		x += 1
l.append('2')
c = 0
d = 0
for i in range(len(l) - 1):
	if d == 0:
		if l[i] == '1':
			c += 1
			x -= 1
			if x == 0:
				break
			if l[i + 1] == '1':
				d = 1
			else:
				c += 1
	else:
		c += 1
		x -= 1
		if x == 0:
			break
		if l[i + 1] == '0':
			c += 1
			d = 0
print(c)


n = int(input())
a = [i for i in ''.join(list(input().split())).split('0') if i != '']
ans = 0
if len(a) == 0:
	print(0)
else:
	if len(a[0]) > 1:
		ans += len(a[0]) + 1
	else:
		ans += 2
	if len(a) == 1:
		print(ans - 1)
	else:
		for i in range(1, len(a) - 1):
			if len(a[i]) == 1:
				ans += 2
			else:
				ans += len(a[i]) + 1
		if len(a[-1]) > 1:
			ans += len(a[-1])
		else:
			ans += 1
		print(ans)


import math
import os
import random
import re
import sys
import functools
from operator import itemgetter, attrgetter
from collections import Counter
Y = lambda : list(map(int, input().split()))
P = lambda : map(int, input().split())
N = lambda : int(input())
n = N()
a = Y()
(f, cnt) = (0, 0)
for i in a:
	if i:
		cnt += 1
		f = 1
	else:
		if f:
			cnt += 1
		f = 0
print(cnt if a[n - 1] or not cnt else cnt - 1)


input()
(s, k) = (input(), 0)
while '1 1' in s:
	(s, k) = (s.replace('1 1', '1', 1), k + 1)
print(k + max(0, 2 * s.count('1') - 1))


n = int(input())
l = input().replace(' ', '').strip('0').split('0')
c = 0
for i in range(len(l)):
	if l[i] != '':
		c += l[i].count('1') + 1
print(max(0, c - 1))


import sys
import math
import bisect
import itertools
import random
import re

def main():
	n = int(input())
	A = list(map(int, input().split()))
	B = []
	cnt = 0
	for i in range(n):
		if A[i] == 1:
			cnt += 1
		else:
			cnt = 0
		if i == n - 1 or A[i + 1] == 0:
			if cnt:
				B.append(cnt)
	ans = 0
	for a in B:
		ans += 2 + a - 1
	if ans:
		ans -= 1
	print(ans)
main()


import sys, threading, os.path
import string
import collections, heapq, math, bisect
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('in.txt'):
		input = open('in.txt', 'r')
	else:
		input = sys.stdin
	n = int(input.readline())
	letters = list(map(int, input.readline().split()))
	num_of_op = 0
	for (i, x) in enumerate(letters):
		if x == 1:
			if num_of_op > 0 and letters[i - 1] == 0:
				num_of_op += 1
			num_of_op += 1
	output = num_of_op
	if os.path.exists('out.txt'):
		open('out.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


n = int(input())
a = [int(i) for i in input().split()]
x = 0
h = 0
for i in range(n):
	if a[i] == 1:
		if x == 0:
			h += 1
			x = 1
		elif x == 1:
			h += 1
	if a[i] == 0 and 1 in a[i:]:
		if x == 1:
			h += 1
			x = 0
print(h)


n = input()
l = input().split()
act = 1
total = 0
for x in l:
	if x == '1':
		total += act
		act = 1
	else:
		act = 1 if total == 0 else 2
print(total)


from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations
from math import gcd, floor, inf
from bisect import bisect_right, bisect_left
n = input()
arr = list(map(int, input().split()))
ans = 0
cnt_grp = 0
for (char, grp) in groupby(arr):
	if char == 1:
		cnt_grp += 1
		ans += len(list(grp))
print(ans + max(cnt_grp - 1, 0))


n = int(input())
s = list(input().split())
t = False
ans = 0
counter = 0
for i in range(n):
	if s[i] == '1':
		ans += 1
		counter += 1
		t = True
	if s[i] == '0' and t is True:
		if i != n - 1:
			ans += 1
			t = False
if t is False and counter != 0:
	ans -= 1
print(ans)


from collections import Counter
import string
import math
import sys

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(number_of_variables):
	if number_of_variables == 1:
		return int(sys.stdin.readline())
	if number_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
mod = 100000007
n = vary(1)
num = array_int()
k = 0
count = 0
for i in range(n):
	if num[i] == 1:
		if k == 0 or k == 1:
			k = 1
			count += 1
		else:
			count += 2
			k = 1
	elif k == 1 or k == 2:
		k = 2
		continue
print(count)


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


n = int(input())
arr = list(map(int, input().split()))
unread = []
for i in range(n):
	if arr[i] == 1:
		unread.append(i)
if len(unread) == 0:
	print(0)
elif len(unread) == 1:
	print(1)
else:
	count = 0
	for i in range(len(unread)):
		if i == 0:
			count += 1
		elif unread[i] - unread[i - 1] <= 2:
			count += unread[i] - unread[i - 1]
		else:
			count += 2
	print(count)


from collections import deque
n = int(input())
l = list(map(int, input().split()))
d = deque()
if len(set(l)) == 1 and l[0] == 0:
	print(0)
	exit(0)
for i in range(n):
	if l[i] == 1:
		d.append(i)
cnt = 0
for i in range(1, len(d)):
	v = d[i] - d[i - 1]
	if v > 2:
		cnt += 2
	else:
		cnt += v
print(cnt + 1)


n = int(input())
y = [int(i) for i in input().split()]
y.append(0)
t = 0
totalone = y.count(1)
countone = 0
for i in range(n):
	if y[i] == 1 and y[i + 1] == 1:
		t += 1
		countone += 1
	elif y[i] == 1 and countone == 0 and (totalone >= 2):
		t += 2
		countone += 1
	elif y[i] == 1 and countone < totalone - 1 and (y[i - 1] == 0) and (totalone >= 2):
		t += 2
		countone += 1
	elif y[i] == 1 and countone < totalone - 1 and (y[i - 1] == 1) and (totalone >= 2):
		t += 2
		countone += 1
if t >= 1 or totalone == 1:
	t += 1
print(t)