#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main(void) {
  int n;
  scanf("%d", &n);
  int uni = 0;
  int free = 0;
  int first = 1;
  int i;
  for (i = 0; i < n; i++) {
    int x;
    scanf("%d", &x);
    if (first && x == 1) {
      uni++;
      first = 0;
    } else if (!first) {
      if (x == 1) {
        uni++;
        if (free <= 1)
          uni += free;
        free = 0;
      } else {
        free++;
      }
    }
  }
  printf("%d\n", uni);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1202 <main+0xe2>
lea    0xe92(%rip),%rdi
lea    -0x1c(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x0,-0x14(%rbp)
je     11a0 <main+0x80>
cmpl   $0x1,-0x1c(%rbp)
jne    11a0 <main+0x80>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
jmp    11ef <main+0xcf>
cmpl   $0x0,-0x14(%rbp)
jne    11ea <main+0xca>
cmpl   $0x1,-0x1c(%rbp)
jne    11dc <main+0xbc>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0x10(%rbp)
jg     11d0 <main+0xb0>
mov    -0x10(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
jmp    11e5 <main+0xc5>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11ea <main+0xca>
jmp    11ef <main+0xcf>
jmp    11f4 <main+0xd4>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115b <main+0x3b>
mov    -0xc(%rbp),%esi
lea    0xdf7(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xece(%rip),%rdi
mov    %rsp,%rsi
call   1040 <scanf@plt>
cmpl   $0x0,(%rsp)
jle    11b4 <main+0x94>
mov    $0x1,%ebx
xor    %ebp,%ebp
lea    0xeb2(%rip),%r14
lea    0x4(%rsp),%r15
xor    %r12d,%r12d
xor    %r13d,%r13d
jmp    1173 <main+0x53>
nopl   0x0(%rax,%rax,1)
mov    $0x1,%eax
test   %ebx,%ebx
je     1193 <main+0x73>
add    $0x1,%ebp
mov    %eax,%ebx
cmp    (%rsp),%ebp
jge    11b7 <main+0x97>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <scanf@plt>
mov    0x4(%rsp),%ecx
test   %ebx,%ebx
je     1160 <main+0x40>
cmp    $0x1,%ecx
jne    1160 <main+0x40>
add    $0x1,%r13d
xor    %eax,%eax
jmp    1169 <main+0x49>
cmp    $0x1,%ecx
jne    11ae <main+0x8e>
xor    %eax,%eax
cmp    $0x2,%r12d
cmovge %eax,%r12d
add    %r12d,%r13d
add    $0x1,%r13d
xor    %r12d,%r12d
jmp    1169 <main+0x49>
add    $0x1,%r12d
jmp    118f <main+0x6f>
xor    %r13d,%r13d
lea    0xe45(%rip),%rdi
mov    %r13d,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %3
store i32 0, i32* %4
store i32 1, i32* %5
store i32 0, i32* %6
br label %9
9: ; preds = %45, %0
%10 = load i32, i32* %6
%11 = load i32, i32* %2
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %48
13: ; preds = %9
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%15 = load i32, i32* %5
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, 1
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
store i32 0, i32* %5
br label %44
23: ; preds = %17, %13
%24 = load i32, i32* %5
%25 = icmp ne i32 %24, 0
br i1 %25, label %43, label %26
26: ; preds = %23
%27 = load i32, i32* %7
%28 = icmp eq i32 %27, 1
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = load i32, i32* %3
%31 = add nsw i32 %30, 1
store i32 %31, i32* %3
%32 = load i32, i32* %4
%33 = icmp sle i32 %32, 1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* %4
%36 = load i32, i32* %3
%37 = add nsw i32 %36, %35
store i32 %37, i32* %3
br label %38
38: ; preds = %34, %29
store i32 0, i32* %4
br label %42
39: ; preds = %26
%40 = load i32, i32* %4
%41 = add nsw i32 %40, 1
store i32 %41, i32* %4
br label %42
42: ; preds = %39, %38
br label %43
43: ; preds = %42, %23
br label %44
44: ; preds = %43, %20
br label %45
45: ; preds = %44
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %9
48: ; preds = %9
%49 = load i32, i32* %3
%50 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %49)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = bitcast i32* %1 to i8*
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%5 = load i32, i32* %1
%6 = icmp sgt i32 %5, 0
br i1 %6, label %7, label %37
7: ; preds = %0
%8 = bitcast i32* %2 to i8*
br label %9
9: ; preds = %7, %30
%10 = phi i32 [ 0, %7 ], [ %34, %30 ]
%11 = phi i32 [ 1, %7 ], [ %33, %30 ]
%12 = phi i32 [ 0, %7 ], [ %32, %30 ]
%13 = phi i32 [ 0, %7 ], [ %31, %30 ]
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%15 = icmp ne i32 %11, 0
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 1
%18 = select i1 %15, i1 %17, i1 false
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = add nsw i32 %13, 1
br label %30
21: ; preds = %9
br i1 %15, label %30, label %22
22: ; preds = %21
br i1 %17, label %23, label %28
23: ; preds = %22
%24 = add nsw i32 %13, 1
%25 = icmp slt i32 %12, 2
%26 = select i1 %25, i32 %12, i32 0
%27 = add nsw i32 %24, %26
br label %30
28: ; preds = %22
%29 = add nsw i32 %12, 1
br label %30
30: ; preds = %21, %28, %23, %19
%31 = phi i32 [ %20, %19 ], [ %13, %21 ], [ %27, %23 ], [ %13, %28 ]
%32 = phi i32 [ %12, %19 ], [ %12, %21 ], [ 0, %23 ], [ %29, %28 ]
%33 = phi i32 [ 0, %19 ], [ 1, %21 ], [ 0, %23 ], [ 0, %28 ]
%34 = add nuw nsw i32 %10, 1
%35 = load i32, i32* %1
%36 = icmp slt i32 %34, %35
br i1 %36, label %9, label %37
37: ; preds = %30, %0
%38 = phi i32 [ 0, %0 ], [ %31, %30 ]
%39 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %38)
ret i32 0
}