#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, has_class, ans = 0, last_class = -1;
  scanf("%d", &N);
  for (int t = 0; t < N; t++) {
    scanf("%d", &has_class);
    if (has_class) {
      if (last_class != -1 && t - last_class == 2)
        ans++;
      ans++;
      last_class = t;
    }
  }
  printf("%d\n", ans);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
lea    0xebc(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11be <main+0x9e>
lea    0xe99(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
cmpl   $0x0,-0xc(%rbp)
je     11ab <main+0x8b>
cmpl   $0xffffffff,-0x14(%rbp)
je     119c <main+0x7c>
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
cmp    $0x2,%eax
jne    119c <main+0x7c>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
jmp    11b0 <main+0x90>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1154 <main+0x34>
mov    -0x10(%rbp),%esi
lea    0xe3b(%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   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xecd(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
jle    1199 <main+0x79>
mov    $0xffffffff,%ebx
xor    %ebp,%ebp
lea    0xeae(%rip),%r14
lea    0xc(%rsp),%r15
xor    %r12d,%r12d
jmp    1169 <main+0x49>
nopl   0x0(%rax)
add    $0x1,%ebp
cmp    0x8(%rsp),%ebp
jge    119c <main+0x7c>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
je     1160 <main+0x40>
cmp    $0xffffffff,%ebx
setne  %al
add    $0x2,%ebx
cmp    %ebp,%ebx
sete   %cl
and    %al,%cl
movzbl %cl,%eax
add    %eax,%r12d
add    $0x1,%r12d
mov    %ebp,%ebx
jmp    1160 <main+0x40>
xor    %r12d,%r12d
lea    0xe60(%rip),%rdi
mov    %r12d,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
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
store i32 0, i32* %1
store i32 0, i32* %4
store i32 -1, i32* %5
%7 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
store i32 0, i32* %6
br label %8
8: ; preds = %32, %0
%9 = load i32, i32* %6
%10 = load i32, i32* %2
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %35
12: ; preds = %8
%13 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%14 = load i32, i32* %3
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %12
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, -1
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = load i32, i32* %6
%21 = load i32, i32* %5
%22 = sub nsw i32 %20, %21
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* %4
%26 = add nsw i32 %25, 1
store i32 %26, i32* %4
br label %27
27: ; preds = %24, %19, %16
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
%30 = load i32, i32* %6
store i32 %30, i32* %5
br label %31
31: ; preds = %27, %12
br label %32
32: ; preds = %31
%33 = load i32, i32* %6
%34 = add nsw i32 %33, 1
store i32 %34, i32* %6
br label %8
35: ; preds = %8
%36 = load i32, i32* %4
%37 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %36)
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 = bitcast i32* %2 to i8*
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%6 = load i32, i32* %1
%7 = icmp sgt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %26, %0
%9 = phi i32 [ 0, %0 ], [ %27, %26 ]
%10 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %9)
ret i32 0
11: ; preds = %0, %26
%12 = phi i32 [ %29, %26 ], [ 0, %0 ]
%13 = phi i32 [ %28, %26 ], [ -1, %0 ]
%14 = phi i32 [ %27, %26 ], [ 0, %0 ]
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%16 = load i32, i32* %2
%17 = icmp eq i32 %16, 0
br i1 %17, label %26, label %18
18: ; preds = %11
%19 = icmp ne i32 %13, -1
%20 = sub nsw i32 %12, %13
%21 = icmp eq i32 %20, 2
%22 = select i1 %19, i1 %21, i1 false
%23 = zext i1 %22 to i32
%24 = add i32 %14, 1
%25 = add i32 %24, %23
br label %26
26: ; preds = %11, %18
%27 = phi i32 [ %25, %18 ], [ %14, %11 ]
%28 = phi i32 [ %12, %18 ], [ %13, %11 ]
%29 = add nuw nsw i32 %12, 1
%30 = load i32, i32* %1
%31 = icmp slt i32 %29, %30
br i1 %31, label %11, label %8
}