N = int(input()) A = list(map(int, input().split())) import re def get_idxs(string, word): idxs = [] for res in re.finditer(word, string): idxs.append(res.span()[0]) return idxs inds = get_idxs("".join(map(str, A)), "1") ans = [0] * len(inds) for i in range(len(inds))[::-1]: if i == len(inds) - 1: if inds[i] == N - 1: continue elif inds[i] == N - 2: ans[i] = 1 else: ans[i] = 2 else: if inds[i] + 2 == inds[i + 1]: ans[i] += 2 + ans[i + 1] elif inds[i] + 1 == inds[i + 1]: ans[i] = 1 else: ans[i] = 2 print(sum(ans))