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.

Difficulty: interview

Starter code:

Solutions:
input()
print(max(0, sum(len(s) + 1 for s in ''.join(input().split()).split('0') if 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
	else:
		if pred==1:
			s+=1
	pred=a[i]
if a[-1]==0:
	s-=1
if s<0:
	s=0
print(s)

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



n = input()
lst = list(map(int, input().split()))

seqs = 0
total = 0
prev = 0

for i in lst:
    if prev:
        if i:
            total += 1
        else:
            prev = 0
    else:
        if i:
            prev = 1
            seqs += 1
            total += 1
if total:
    print(seqs + total - 1)
else:
    print(0)


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
    else:
        if l == 1:
            ans += 1
        else:
            ans += 1
            inbox = 1
if ans > 0 and let[n-1] == 0:
    ans -= 1
print(ans)


#!/usr/bin/env python3

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()

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)

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)


#input
n=int(input())
letter=str(input())

#variables

#main
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)

# Codeforces 265 Div. 2
# B

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 = 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)
    


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
else:
    if len(ar)>0:
        ar.remove(ar[0])


#print(ar)

totalread = 0
totalmove = len(ar)

for i in range(n):
    if array[i]==1:
        totalread+=1


print(totalread+totalmove)

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
        else:
            if 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())
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())
s = list(map(int, input().split()))

ids = []
for i in range(n):
    if s[i] == 1:
        ids.append(i)
#print(ids)

x = len(ids)
c = 1
if len(ids) == 0:
    print(0)
else:
    for i in range(x-1):
        
        diff = ids[i+1] - ids[i]
        #print(diff, 'x')
        #print(c)
        if diff > 2 :
            c += 2
            #print('t')
        else:
            c += diff
        #print(c, i)
        
    print(c)


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())
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())

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())
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)

import re


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

    print(len(s))


def __starting_point():
    main()


__starting_point()

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)

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)


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))

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)
    return

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)