AtCoder Beginner Contest 140 - Maximal Value

There is an integer sequence A of length N whose values are unknown.
Given is an integer sequence B of length N-1 which is known to satisfy the following:
 B_i \geq \max(A_i, A_{i+1}) 
Find the maximum possible sum of the elements of A.

-----Constraints-----
 - All values in input are integers.
 - 2 \leq N \leq 100
 - 0 \leq B_i \leq 10^5

-----Input-----
Input is given from Standard Input in the following format:
N
B_1 B_2 ... B_{N-1}

-----Output-----
Print the maximum possible sum of the elements of A.

-----Sample Input-----
3
2 5

-----Sample Output-----
9

A can be, for example, ( 2 , 1 , 5 ), ( -1 , -2 , -3 ), or ( 2 , 2 , 5 ). Among those candidates, A = ( 2 , 2 , 5 ) has the maximum possible sum.

Source: atcoder | Difficulty: EASY


Solutions:
N = int(input())
B = list(map(int, input().split()))
sumA = B[0] + B[N - 2]
for i in range(N - 2):
	sumA += min(B[i], B[i + 1])
print(sumA)


n = int(input())
b = list(map(int, input().split()))
tot = b[0] + b[-1]
for i in range(n - 2):
	tot += min(b[i], b[i + 1])
print(tot)


n = int(input())
b = [int(_) for _ in input().split()]
print(b[0] + b[n - 2] + sum((min(b[i], b[i - 1]) for i in range(1, n - 1))))


n = int(input())
b = [int(x) for x in input().split()]
a = [0] * n
a[0] = b[0]
a[n - 1] = b[n - 2]
for i in range(1, n - 1):
	s = min(b[i], b[i - 1])
	a.append(s)
print(sum(a))


N = int(input())
B = [10 ** 10] + list(map(int, input().split())) + [10 ** 10]
K = 0
for i in range(0, N):
	K += min(B[i], B[i + 1])
print(K)


n = int(input())
b = tuple(map(int, input().split()))
ans = b[0] + b[-1] + sum((min(x, y) for (x, y) in zip(b, b[1:])))
print(ans)


n = int(input())
b = [int(j) for j in input().split()]
ans = b[0] + b[-1]
for i in range(n - 2):
	ans += min(b[i], b[i + 1])
print(ans)


N = int(input())
B = list(map(int, input().split()))
s = B[0]
for j in range(N - 2):
	s += min(B[j], B[j + 1])
s += B[N - 2]
print(s)


n = int(input())
b = list(map(int, input().split()))
print(sum([b[0]] + [min(b[i - 1], b[i]) for i in range(1, n - 1)] + [b[-1]]))


(n, *a) = map(int, open(0).read().split())
print(sum((min(a[i], a[i + 1]) for i in range(n - 2))) + a[0] + a[-1])


n = int(input())
b = [int(i) for i in input().split()]
a = b[0] + b[-1]
for i in range(n - 2):
	a += min(b[i], b[i + 1])
print(a)


N = int(input())
B = tuple(map(int, input().split()))
result = B[0] + B[-1]
for i in range(N - 2):
	result += min(B[i], B[i + 1])
print(result)


N = int(input())
B = input().split()
ans = 0
for i in range(N):
	if i == 0:
		ans += int(B[i])
	elif i == N - 1:
		ans += int(B[i - 1])
	else:
		ans += min(int(B[i]), int(B[i - 1]))
print(ans)


N = int(input())
b = list(map(int, input().split()))
a = [0] * N
a[0] = b[0]
a[N - 1] = b[N - 2]
for i in range(N - 2):
	a[i + 1] = min(b[i], b[i + 1])
print(sum(a))


n = int(input())
B = list(map(int, input().split()))
s = B[0] + B[n - 2]
for i in range(n - 2):
	s += min(B[i], B[i + 1])
print(s)


n = int(input())
blst = list(map(int, input().split()))
alst = [blst[0]]
for i in range(n - 2):
	alst.append(min(blst[i], blst[i + 1]))
alst.append(blst[-1])
print(sum(alst))


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


N = int(input())
B = [9 ** 9] + list(map(int, input().split())) + [9 ** 9]
ans = 0
for i in range(len(B) - 1):
	ans += min(B[i], B[i + 1])
print(ans)


N = int(input())
B = list(map(int, input().split()))
B.append(1000000)
A = 0
for i in range(0, N):
	A += min(B[i], B[i - 1])
print(A)


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


n = int(input())
b = [int(i) for i in input().split()]
A = [b[0]] + [min(b[i], b[i - 1]) for i in range(1, n - 1)] + [b[-1]]
print(sum(A))


N = int(input())
B = list(map(int, input().split()))
A = [0] * N
for i in range(N):
	A[i] = B[i - 1]
	if A[i - 1] > B[i - 1]:
		A[i - 1] = B[i - 1]
A[0] = B[0]
print(sum(A))


N = int(input())
B_ls = [float('inf')] + list(map(int, input().split(' '))) + [float('inf')]
A_ls = []
for i in range(N):
	A_ls.append(min(B_ls[i], B_ls[i + 1]))
print(sum(A_ls))


(_, b) = open(0)
(*b,) = map(int, b.split())
print(b[0] + sum((min(t) for t in zip(b, b[1:]))) + b[-1])


n = int(input())
b = list(map(int, input().split()))
l = []
for i in range(n - 2):
	l.append(min(b[i], b[i + 1]))
print(sum(l) + b[0] + b[n - 2])


n = int(input())
b = [int(i) for i in input().split()]
ans = b[-1] + b[0]
for (i, j) in zip(b[:-1], b[1:]):
	ans += min(i, j)
print(ans)


n = int(input())
b = [int(i) for i in input().split()]
prev = b[0]
cnt = 0
for i in b:
	cnt += min(i, prev)
	prev = i
cnt += b[-1]
print(cnt)


n = int(input())
a = list(map(int, input().split())) + [100000]
print(sum([a[0]] + [min(a[i], a[i + 1]) for i in range(n - 1)]))


N = int(input())
B = list(map(int, input().strip().split()))
ans = B[0] + B[N - 2]
for n in range(N - 2):
	ans += min(B[n], B[n + 1])
print(ans)


N = int(input())
B = [int(b) for b in input().split()]
A = [0] * N
A[0] = B[0]
A[1] = B[0]
for i in range(N - 1):
	if B[i] >= max(A[i], A[i]):
		A[i + 1] = B[i]
	else:
		A[i] = B[i]
		A[i + 1] = B[i]
print(sum(A))


n = int(input())
a = list(map(int, input().split()))
res = a[-1]
for i in range(n - 3, -1, -1):
	res += min(a[i], a[i + 1])
print(res + a[0])


n = int(input())
B = list(map(int, input().split()))
ans = 0
p = 10 ** 9
for b in B:
	ans += min(p, b)
	p = b
ans += b
print(ans)


n = int(input())
L = list(map(int, input().split()))
sum = L[0] + L[n - 2]
for i in range(n - 2):
	a = min(L[i], L[i + 1])
	sum += a
print(sum)


N = int(input())
B = list(map(int, input().split()))
A = list([0] * N)
for i in range(N - 2):
	A[i + 1] = min(B[i], B[i + 1])
A[0] = B[0]
A[N - 1] = B[N - 2]
print(sum(A))


N = int(input())
b = list(map(int, input().split()))
ans = b[0] + b[-1]
for i in range(N - 2):
	ans += min(b[i], b[i + 1])
print(ans)


N = int(input())
B = list((int(x) for x in input().split()))
A1 = B + [100001]
A2 = [100001] + B
A = [min(A1[i], A2[i]) for i in range(N)]
print(sum(A))


import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
input = sys.stdin.readline

def RD():
	return input().rstrip()

def F():
	return float(input().rstrip())

def I():
	return int(input().rstrip())

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

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

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

def TI():
	return tuple(map(int, input().split()))

def LF():
	return list(map(float, input().split()))

def Init(H, W, num):
	return [[num for i in range(W)] for j in range(H)]

def main():
	N = I()
	B = LI()
	ans = B[-1] + B[0]
	for i in range(N - 2):
		ans += min(B[i], B[i + 1])
	print(ans)

def __starting_point():
	main()
__starting_point()


N = int(input())
B = list(map(int, input().split()))
print(sum(map(min, B, B[1:])) + B[0] + B[-1])


N = int(input())
A = list(map(int, input().split()))
count = A[0] + A[N - 2]
for i in range(1, N - 1):
	count += min(A[i - 1], A[i])
print(count)


n = int(input())
b = list(map(int, input().split()))
print(sum((min(i, j) for (i, j) in zip([b[0]] + b, b + [b[-1]]))))


N = int(input())
(*B,) = map(int, input().split())
ans = B[0] + B[-1] + sum((min(b0, b1) for (b0, b1) in zip(B[:-1], B[1:])))
print(ans)


N = int(input())
A = []
B = [int(i) for i in input().split()]
ans = 0
for i in range(N):
	if i == 0:
		A.append(B[0])
		continue
	if i == N - 1:
		A.append(B[N - 2])
		continue
	a = B[i - 1]
	b = B[i]
	if a < b:
		A.append(a)
	else:
		A.append(b)
for i in A:
	ans += i
print(ans)


n = int(input())
b = list(map(int, input().split())) + [100100]
a = [-1] * n
for i in range(n):
	a[i] = min(b[i], b[i - 1])
print(sum(a))


import sys
input = sys.stdin.readline

def main():
	numbers = []
	n = int(input())
	numbers = list(map(int, input().split()))
	dp = [0] * n
	dp[0] = numbers[0]
	dp[-1] = numbers[-1]
	for i in range(1, n - 1):
		dp[i] = min(numbers[i - 1], numbers[i])
	print(sum(dp))

def __starting_point():
	main()
__starting_point()


N = int(input())
B = list(map(int, input().split()))
A = [B[0], B[0]]
for i in range(1, N - 1):
	b = B[i]
	A.append(b)
	a = max([A[i], A[i + 1]])
	if a > b:
		A[A.index(a)] = b
print(sum(A))


n = int(input())
b = [int(i) for i in input().split()]
c = b[0] + b[-1]
for i in range(1, n - 1):
	c += min(b[i], b[i - 1])
print(c)


n = int(input())
b = list(map(int, input().split()))
b.insert(0, 10000000.0)
b.append(10000000.0)
count = 0
for i in range(n):
	count += min(b[i], b[i + 1])
print(count)


N = int(input())
B = list(map(int, input().split()))
ans = B[0] + B[-1]
for (a, b) in zip(B, B[1:]):
	ans += min(a, b)
print(ans)


import math
import collections
import itertools

def resolve():
	N = int(input())
	B = list(map(int, input().split()))
	cnt = B[0]
	for i in range(N - 2):
		if B[i] <= B[i + 1]:
			cnt += B[i]
		else:
			cnt += B[i + 1]
	cnt += B[N - 2]
	print(cnt)
resolve()


n = int(input())
B = list(map(int, input().split()))
A = [0] * n
A[0] = B[0]
A[n - 1] = B[n - 2]
if n >= 3:
	for i in range(1, n - 1):
		A[i] = min(B[i], B[i - 1])
print(sum(A))


n = int(input())
bl = list(map(int, input().split()))
al = [0] * n
al[0] = bl[0]
al[-1] = bl[-1]
for i in range(n - 2):
	al[i + 1] = min(bl[i], bl[i + 1])
print(sum(al))


n = int(input())
(*b,) = map(int, input().split())
s = b[0]
for i in range(1, n - 1):
	s += min(b[i - 1], b[i])
s += b[-1]
print(s)


n = int(input())
B = list(map(int, input().split()))
tmp = B[0]
ans = 0
B.append(B[-1])
for b in B:
	ans += min(tmp, b)
	tmp = b
print(ans)


n = int(input())
b = [0] * (n - 1)
b[:] = map(int, input().split())
r = b[0]
for ii in range(n - 2):
	r += min(b[ii], b[ii + 1])
r += b[n - 2]
print(r)


n = int(input())
bn = list(map(int, input().split()))
an = [bn[0]]
for x in range(n - 2):
	if bn[x + 1] >= bn[x]:
		an.append(bn[x])
	else:
		an.append(bn[x + 1])
an.append(bn[-1])
print(sum(an))


N = int(input())
B = [int(i) for i in input().split()]
ans = B[0] + B[-1]
if N >= 3:
	for i in range(0, N - 2):
		ans += min(B[i], B[i + 1])
print(ans)


N = int(input())
ls = [int(i) for i in input().split()]
if len(ls) == 1:
	print(ls[0] * 2)
else:
	als = [ls[0]]
	for n in range(1, N - 1):
		als.append(min(ls[n - 1], ls[n]))
	als.append(ls[-1])
	print(sum(als))


N = int(input())
B = [10 ** 6] + list(map(int, input().split())) + [10 ** 6]
print(sum([min(B[i], B[i + 1]) for i in range(N)]))


n = int(input())
b = list(map(int, input().split(' ')))
(m, j) = [b[0] + b[n - 2], 0]
for i in b:
	m += min(i, j)
	j = i
print(m)


N = int(input())
B = [int(n) for n in input().split()][::-1]
print(sum([B[0], B[N - 2]] + [min(B[i - 1], B[i]) for i in range(1, N - 1)]))


(n, *l) = map(int, open(0).read().split())
(s, p) = (0, l[0])
for i in l:
	s += min(p, i)
	p = i
print(s + p)


N = int(input())
B = tuple(map(int, input().split()))
result = 0
tmp = 0
for i in B:
	result += min(tmp, i)
	tmp = i
result += B[0] + B[-1]
print(result)


N = int(input())
B = [int(x) for x in input().split()]
res = B[0] + B[-1]
for i in range(0, N - 2):
	res += min(B[i], B[i + 1])
print(res)


n = int(input())
b = list(map(int, input().split()))
c = 0
for i in range(len(b) - 1):
	c += min(b[i], b[i + 1])
print(c + b[0] + b[-1])


INF = 10 ** 6
N = int(input())
B = [INF] + list(map(int, input().split())) + [INF]
ans = 0
for i in range(1, N + 1):
	ans += min(B[i - 1], B[i])
print(ans)


n = int(input())
B = list(map(int, input().split()))
print(B[0] + B[-1] + sum((min(B[i], B[i + 1]) for i in range(len(B) - 1))))


def resolve():
	n = int(input())
	b = list(map(int, input().split()))[::-1]
	ans = b[0] + b[-1]
	for i in range(n - 2):
		ans += min(b[i], b[i + 1])
	print(ans)
resolve()


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


_ = input()
B = list(map(int, input().split()))
v = B[-1]
pb = 10 ** 5
for b in B:
	v += min(b, pb)
	pb = b
print(v)


input()
vs = list(map(int, input().split()))
print(vs[0] + sum((min(x, y) for (x, y) in zip(vs[:-1], vs[1:]))) + vs[-1])


N = int(input())
A = [int(x) for x in input().split()]
B = [A[0]]
for i in range(len(A)):
	if B[i] > A[i]:
		B[i] = A[i]
	B.append(A[i])
print(sum(B))


N = int(input())
B = list(map(int, input().split()))
m = B[0]
for i in range(1, N - 1):
	m += min(B[i - 1], B[i])
print(m + B[-1])


n = int(input())
b = [10 ** 6] + list(map(int, input().split())) + [10 ** 6]
ans = 0
for i in range(1, n + 1):
	ans += min(b[i - 1], b[i])
print(ans)


import sys
sys.setrecursionlimit(10 ** 8)
ini = lambda : int(sys.stdin.readline())
inl = lambda : [int(x) for x in sys.stdin.readline().split()]
ins = lambda : sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print('\x1b[33m', *a, '\x1b[0m', **dict(file=sys.stderr, **kw))

def solve():
	n = ini()
	B = inl()
	A = [None] * n
	A[0] = B[0]
	for i in range(1, n - 1):
		A[i] = min(B[i - 1], B[i])
	A[n - 1] = B[n - 2]
	return sum(A)
print(solve())


a = int(input())
f = list(map(int, input().split()))
ans = [f[0], f[-1]]
for i in range(a - 2):
	ans.append(min(f[i], f[i + 1]))
print(sum(ans))


n = int(input())
B = [10 ** 6] + list(map(int, input().split())) + [10 ** 6]
s = 0
for i in range(n):
	s += min(B[i], B[i + 1])
print(s)


input()
B = list(map(int, input().split()))
print(sum([min(x, y) for (x, y) in zip(B + [10 ** 5 + 1], [B[0]] + B)]))


n = int(input())
b = [10 ** 18] + list(map(int, input().split())) + [10 ** 18]
a = [0] * n
for i in range(n):
	a[i] = min(b[i], b[i + 1])
print(sum(a))


N = int(input())
inf = 10 ** 10
(*a,) = map(int, input().split())
b = [inf] + a + [inf]
print(sum((min(b[i], b[i + 1]) for i in range(N))))


(_, b) = open(0)
(*b,) = map(int, b.split())
print(b[0] + b[-1] + sum((min(i, j) for (i, j) in zip(b, b[1:]))))


N = int(input())
B = list(map(int, input().split()))
print(B[0] + sum([min(i, j) for (i, j) in zip(B, B[1:])]) + B[-1])


input()
b = list(map(int, input().split()))
print(sum(map(min, [b[0]] + b, b + [b[-1]])))


N = int(input())
B = [int(i) for i in input().split()]
A = [B[0]] + [min(B[i - 1], B[i]) for i in range(1, N - 1)] + [B[-1]]
print(sum(A))


n = int(input())
l = list(map(int, input().split()))
ret = l[0] + l[-1]
for i in range(1, n - 1):
	ret += min(l[i], l[i - 1])
print(ret)


def main():
	N = int(input())
	ans = 0
	B = list(map(int, input().split()))
	ans += B[0] + B[-1]
	for i in range(1, N - 1):
		ans += min(B[i - 1], B[i])
	print(ans)

def __starting_point():
	main()
__starting_point()


N = int(input())
B = list(map(int, input().split()))
B += [max(B)]
print(sum((min(B[i - 1], B[i]) for i in range(N))))


n = int(input())
x = [int(i) for i in input().split()]
ans = x[0] + x[-1]
for i in range(n - 2):
	ans += min(x[i], x[i + 1])
print(ans)


_ = input()
B = (10 ** 5,) + tuple(map(int, input().split())) + (10 ** 5,)
print(sum((min(b) for b in zip(B, B[1:]))))


N = int(input())
L = list(map(int, input().split()))
s = L[0]
for i in range(len(L) - 1):
	s += min(L[i], L[i + 1])
print(s + L[N - 2])


a = input()
b = input().split()
sum = int(b[0])
for i in range(len(b) - 1):
	sum += min(int(b[i]), int(b[i + 1]))
sum += int(b[-1])
print(sum)


n = int(input())
b = list(map(int, input().split()))
inf = 10000000
print(sum((min(b1, b2) for (b1, b2) in zip(b + [inf], [inf] + b))))


N = int(input())
B = list(map(int, input().split()))
B = B[::-1]
A = []
for i in range(N):
	if i == 0:
		A.append(B[0])
	elif i >= N - 1:
		A.append(B[i - 1])
	else:
		a = min(B[i], B[i - 1])
		A.append(a)
print(sum(A))


N = int(input())
ls = [int(s) for s in input().split()]
sum = ls[0] + ls[N - 2]
for i in range(N - 2):
	sum += min([ls[i], ls[i + 1]])
print(sum)


n = int(input())
arr = list(map(int, input().split()))
ans = []
for (i, ele) in enumerate(arr):
	if i == 0:
		ans.append(ele)
	else:
		ans.append(min(arr[i - 1], arr[i]))
	if i == len(arr) - 1:
		ans.append(arr[-1])
print(sum(ans))


(_, b) = open(0)
(*b,) = map(int, b.split())
print(b[0] + sum(map(min, zip(b, b[1:]))) + b[-1])


(N, *B) = list(map(int, open(0).read().split()))
ans = B[0] + B[-1]
for i in range(N - 2):
	ans += min(B[i], B[i + 1])
print(ans)


n = int(input())
b = list(map(int, input().split()))
a = []
a.append(b[0])
for i in range(1, n - 1):
	a.append(min(b[i - 1], b[i]))
a.append(b[-1])
num = 0
for i in range(n):
	num += a[i]
print(num)


n = int(input())
bn = [int(num) for num in input().split()]
an = [0] * n
an[0] = bn[0]
an[n - 1] = bn[n - 2]
for i in range(1, n - 1):
	s = min(bn[i], bn[i - 1])
	an.append(s)
print(sum(an))


n = int(input())
a = list(map(int, input().split()))
s = 0
for i in range(n - 2):
	s += min(a[n - 2 - i], a[n - i - 3])
print(s + a[n - 2] + a[0])


n = int(input())
a = [int(x) for x in input().split()]
res = [0] * n
res[0] = a[0]
for i in range(1, n - 1):
	res[i] = min(a[i], a[i - 1])
res[n - 1] = a[-1]
print(sum(res))


N = int(input())
B = [int(i) for i in input().split()]
a = B[0] + B[-1]
for i in range(1, N - 1):
	a += min(B[i - 1], B[i])
print(a)


N = int(input())
B = list(map(int, input().split()))
print(sum([min(B[x], B[x + 1]) for x in range(len(B) - 1)]) + B[0] + B[-1])


N = int(input())
l = list(map(int, input().split()))
ans = 0
last_n = 0
for i in l:
	if i >= last_n:
		ans += i
		last_n = i
	else:
		ans -= last_n
		ans += i * 2
		last_n = i
ans += l[0]
print(ans)


N = int(input())
li = list(map(int, input().split()))
rli = li[::-1]
rli.append(100001)
ans = [0] * N
ans[0] = rli[0]
for i in range(0, len(li)):
	ans[i + 1] = min(rli[i], rli[i + 1])
print(sum(ans))


N = int(input())
B = list(map(int, input().split()))
print(B[0] + B[-1] + sum((min(x, y) for (x, y) in zip(B, B[1:]))))


N = int(input())
B = list(map(int, input().split()))
S = B[0] + B[-1]
for n in range(N - 2):
	S += min(B[n], B[n + 1])
print(S)


N = int(input())
B = [1 << 100] + [int(a) for a in input().split()] + [1 << 100]
print(sum([min(B[i], B[i + 1]) for i in range(N)]))


N = int(input())
B = list(map(int, input().split()))
con = 0
for i in range(N):
	if i == 0:
		con += B[i]
	elif i == N - 1:
		con += B[N - 2]
	else:
		con += min(B[i - 1], B[i])
print(con)


n = int(input())
b = [int(x) for x in input().split()]
s = b[0] + b[n - 2]
for c in range(n - 2):
	s = s + min(b[c + 1], b[c])
print(s)


N = int(input())
A = [0] * N
B = list(map(int, input().split()))
A[0] = B[0]
A[-1] = B[-1]
for n in range(1, N - 1):
	A[n] = min(B[n - 1], B[n])
print(sum(A))


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


n = int(input()) - 1
A = list(map(int, input().split()))
cnt = 0
for i in range(1, n):
	cnt += min(A[i - 1], A[i])
print(cnt + A[0] + A[-1])


N = int(input())
bs = list(map(int, input().split()))
list_a = [bs[0]]
for i in range(1, len(bs)):
	a = min(bs[i - 1], bs[i])
	list_a.append(a)
list_a.append(bs[len(bs) - 1])
print(sum(list_a))


N = int(input())
B = list(map(int, input().split()))
cnt = 0
for i in range(len(B) - 1):
	cnt += min(B[i], B[i + 1])
cnt += B[0] + B[N - 2]
print(cnt)


n = int(input())
b = list(map(int, input().split()))
c = [b[0]]
for i in range(len(b) - 1):
	x = min(b[i], b[i + 1])
	c.append(x)
print(sum(c) + b[n - 2])


n = int(input())
alist = list(map(int, input().split()))
alist.insert(0, 10000000.0)
alist.append(10000000.0)
core = 0
for i in range(n):
	core += min(alist[i], alist[i + 1])
print(core)


(N, *B) = map(int, open(0).read().split())
print(B[0] + sum((min(i, j) for (i, j) in zip(B, B[1:]))) + B[-1])


N = int(input())
B = [int(n) for n in input().split()]
A = [0] * N
A[0] = B[0]
A[-1] = B[-1]
for i in range(1, N - 1):
	A[i] = B[i - 1]
	if A[i] > B[i]:
		A[i] = B[i]
print(sum(A))


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


n = int(input())
b = list(map(int, input().split()))
ans = 0
aa = 0
ab = 100001
for i in range(n - 1):
	aa = min(ab, b[i])
	ab = b[i]
	ans += aa
print(ans + ab)


(n, *bb) = map(int, open(0).read().split())
ans = bb[0] + bb[-1]
for i in range(n - 2):
	ans += min(bb[i], bb[i + 1])
print(ans)


n = int(input())
num_list = list(map(int, input().split()))
a_list = [0] * n
a_list[0] = num_list[0]
for i in range(1, n, 1):
	if i != n - 1:
		a_list[i] = min(num_list[i - 1], num_list[i])
	else:
		a_list[i] = num_list[-1]
sum = 0
for j in a_list:
	sum += j
print(sum)


N = int(input())
B_List = list(map(int, input().split()))
A_List = [[] for i in range(N)]
A_List[0] = B_List[0]
A_List[1] = B_List[0]
for i in range(1, N - 1):
	if B_List[i] <= B_List[i - 1]:
		A_List[i] = B_List[i]
	A_List[i + 1] = B_List[i]
print(sum(A_List))


N = int(input())
(*B,) = map(int, input().split())
ans = B[0] + B[-1]
i = 0
while i + 1 < N - 1:
	ans += min(B[i], B[i + 1])
	i += 1
print(ans)