Problem:


Score : 300 points

Problem Statement

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 1

3
2 5

Sample Output 1

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.

Sample Input 2

2
3

Sample Output 2

6

Sample Input 3

6
0 153 10 10 23

Sample Output 3

53



Sample input:
3
2 5


Sample output:
9

Submission status: Accepted

Solution code:
import math
from collections import deque
from collections import defaultdict

#自作関数群
def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()
def factorization(n):
	res = []
	if n%2==0:
		res.append(2)
	for i in range(3,math.floor(n//2)+1,2):
		if n%i==0:
			c = 0
			for j in res:
				if i%j==0:
					c=1
			if c==0:
				res.append(i)
	return res
def fact2(n):
	p = factorization(n)
	res = []
	for i in p:
		c=0
		z=n
		while 1:
			if z%i==0:
				c+=1
				z/=i
			else:
				break
		res.append([i,c])
	return res
def fact(n):#階乗
	ans = 1
	m=n
	for _i in range(n-1):
		ans*=m
		m-=1
	return ans
def comb(n,r):#コンビネーション
	if n<r:
		return 0
	l = min(r,n-r)
	m=n
	u=1
	for _i in range(l):
		u*=m
		m-=1
	return u//fact(l)
def combmod(n,r,mod):
	return (fact(n)/fact(n-r)*pow(fact(r),mod-2,mod))%mod
def printQueue(q):
	r=copyQueue(q)
	ans=[0]*r.qsize()
	for i in range(r.qsize()-1,-1,-1):
		ans[i] = r.get()
	print(ans)
class UnionFind():
	def __init__(self, n):
		self.n = n
		self.parents = [-1]*n

	def find(self, x): # root
		if self.parents[x]<0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	def union(self,x,y):
		x = self.find(x)
		y = self.find(y)

		if x==y:
			return

		if self.parents[x]>self.parents[y]:
			x,y = y,x

		self.parents[x]+=self.parents[y]
		self.parents[y]=x

	def size(self,x):
		return -1*self.parents[self.find(x)]

	def same(self,x,y):
		return self.find(x)==self.find(y)

	def members(self,x): # much time
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i)==root]

	def roots(self):
		return [i for i,x in enumerate(self.parents) if x<0]

	def group_count(self):
		return len(self.roots())

	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()} # 1~n
def bitArr(n):#ビット全探索
	x = 1
	zero = "0"*n
	ans = []
	ans.append([0]*n)
	for i in range(2**n-1):
		ans.append(list(map(lambda x:int(x),list((zero+bin(x)[2:])[-1*n:]))))
		x+=1
	return ans;
def arrsSum(a1,a2):
	for i in range(len(a1)):
		a1[i]+=a2[i]
	return a1
def maxValue(a,b,v):
	v2 = v
	for i in range(v2,-1,-1):
		for j in range(v2//a+1): #j:aの個数
			k = i-a*j
			if k%b==0:
				return i
	return -1
def copyQueue(q):
	nq = queue.Queue()
	n = q.qsize()
	for i in range(n):
		x = q.get()
		q.put(x)
		nq.put(x)
	return nq
def get_sieve_of_eratosthenes(n):
	#data = [2]
	data = [0,0,0]
	for i in range(3,n+1,2):
		data.append(i)
		data.append(0)
	for i in range(len(data)):
		interval = data[i]
		if interval!=0:
			for j in range(i+interval,n-1,interval):
				data[j] = 0
	#ans = [x for x in data if x!=0]
	ans = data[:]
	return ans

n = readInt()
b = readInts()

ans = [b[0]]

for i in range(1,n):
	ans.append(b[i-1])
	if ans[-2]>ans[-1]:
		ans[-2]=ans[-1]

print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))

if n==2:
    print(b[0]*2)
else:
    ans=[0]*n
    ans[0]=b[0]
    ans[n-1]=b[-1]
    for i in range(1,n-1):
        ans[i]=min(b[i],b[i-1])
    print(sum(ans))

Submission status: Accepted

Solution code:
N = int(input())
B_list = list(map(int, input().split()))

A_list = [0]*(N+1)
A_list[0] = B_list[0]
A_list[1] = B_list[0]

for i in range(1, N-1):
    A_list[i + 1] = B_list[i]
    if A_list[i + 1] < A_list[i]:
        A_list[i] = A_list[i + 1]
    
print(sum(A_list))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
b.reverse()
ret=min(b[0:1])

for i in range(0,n-1):
  ret+=min(b[i:i+2])

  
print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
blist=list(map(int,input().split()))
alist=[blist[-1]]

for i in range(n-1):
    tmp = blist[-(i+1)]
    alist[i] = min(alist[i],tmp)
    alist.append(tmp)

print(sum(alist))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))
A = [0]*(N+1)
A[0] = B[0]
A[N-1] = B[-1]

for i in range(N-2) :
    A[i+1] = min(B[i],B[i+1])

print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = [int(x) for x in input().split()]
res = B[0] + B[-1]

for i in range(len(B) - 1):
    res += min(B[i], B[i+1])

print(res)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
"""
b_i >= max(a_i, a_(i+1))
"""
a = [10**18 for _ in range(n)]
for i in range(n):
	if i == 0:
		a[i] = b[i]
	elif i == n-1:
		a[i] = b[i-1]
	else:
		a[i] = min(b[i], b[i-1])
print(sum(a))

Submission status: Accepted

Solution code:
n = input()
B = list(map(int,input().split()))
A = [B[0]]
for i,b in enumerate(B):
    A[i] = min(A[i],B[i])
    A.append(max(B[i],A[i]))

print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))
ans = B[0]

for i in range(N-2):
    ans += min(B[i], B[i+1])

ans += B[-1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
b = [b[0]] + b + [b[-1]]

a = [0] * n
for i in range(n):
    a[i] = min(b[i], b[i+1])
print(sum(a))

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))


ans=b[0]+b[n-2]

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

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))

ans=B[0]+B[N-2]
for i in range(0,N-2):
    if B[i]<B[i+1]:
        ans+=B[i]
    else:
        ans+=B[i+1]

print(ans)

Submission status: Accepted

Solution code:
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],b[i-1]))
a.append(b[n-2])
print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
l.reverse()

ans =[l[0], l[-1]]

for i in range(n-2):
    ans.append(min(l[i], l[i+1]))

print(sum(ans))

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans_li=[10 ** 5 + 1] * n

ans_li[0] = li[0]
for i in range(len(li)-1):
    ans_li[i] = min(li[i], ans_li[i])
    ans_li[i+1] = min(li[i], li[i+1])
ans_li[-1] = li[-1]

print(sum(ans_li))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
S=list(map(int,input().split()))
Z=[]
Z.append(S[0])
for i in range(N-2):
    Z.append(min(S[i],S[i+1]))
Z.append(S[N-2])
print(sum(Z))

Submission status: Accepted

Solution code:
from sys import stdin, stdout
input = stdin.readline
print = stdout.write

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))

sum = b[n-2] + b[0]
for i in range(n-2):
    #print(i,b[i],b[i+1])
    sum += min(b[i],b[i+1])
 
print(sum)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))

A = [None] * N
A[0] = B[0]
for i in range(N-1):
  if i+1 < len(B):
    A[i+1] = min(B[i], B[i+1])
  else:
    A[i+1] = B[i]

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.buffer.readline

N = int(readline())
B = list(map(int, readline().split()))

B.append(float('inf'))

mins = [B[0]]

for i in range(1, N):
    mins.append(min(B[i-1], B[i]))

print(sum(mins))

Submission status: Accepted

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

print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
n = int(input())
l = list(map(int,input().split()))
if n==2:
    ans = l[0]*2
    print(ans)
    sys.exit()
    
ans = 0
lis = []
a,b = l[0],l[1]
if a<=b:
    ans +=a
    lis.append(a)
else:
    ans += a
    lis.append(b)
    
for i in range(n-2):
    a,b = l[i],l[i+1]
    if a<=b:
        ans += a
        lis.append(a)
    else:
        ans += b
        lis.append(b)
        
ans += l[n-2]
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, *b = map(int, read().split())

a = [b[0]]
for i in range(1, n-1):
    a.append(min(b[i - 1], b[i]))
a.append(b[-1])
print(sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
A = [B[0], B[0]]

for b in B[1:]:
    if A[-1] <= b:
        A += [b]
    else:
        A[-1] = b
        A += [b]
# print(f"A:{A}", file=sys.stderr)
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = lambda: sys.stdin.readline().rstrip()

INF = 10 ** 5 + 1


def solve():
    N = int(input())
    B = list(map(int, input().split()))
    A = [INF] * N

    for i in range(N - 1):
        A[i] = min(A[i], B[i])
        A[i + 1] = min(A[i + 1], B[i])

    print(sum(A))


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
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(1, n-1):
    a[i] = min(b[i-1],b[i])
print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
b = [int(i) for i in input().split()]

s = 0
s = b[0]+b[-1]
for i in range(1, n-1):
  s += min(b[i],b[i-1])
  
print(s)

Submission status: Accepted

Solution code:
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(1,N-1):
  A[i] = min(B[i-1],B[i])
  
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

ans = 0

A = [0] * N
A[0] = B[0]
A[-1] = B[-1]

for i in range(1, N-1):
    min_B = min(B[i-1], B[i])
    A[i] = min_B

print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())

blis = list(map(int, input().split()))

alis = [0]*n

alis[0] = blis[0]

for i in range(1, n-1):
    alis[i] = min(blis[i-1], blis[i])
    
alis[n-1] = blis[n-2]

print(sum(alis))

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


# -------------------------------------------------------------
# main
# -------------------------------------------------------------
N = int(input())
B = list(map(int,input().split()))

A = [0]*N
A[0] = B[0]
for i in range(N-1):
    A[i+1] = B[i]
    if A[i] > B[i]:
        A[i] = B[i]

print(sum(A))

Submission status: Accepted

Solution code:
# input
n = int(input())
a = [0 for i in range(n)]
b = [int(i) for i in input().split()]

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

# output
print(sum(a))

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = [0] * n
    b = list(map(int, input().split()))
    a[0] = b[0]
    a[-1] = b[-1]
    for i in range(n-2):
        a[i+1] = min(b[i], b[i+1])
    return sum(a)

if __name__ == "__main__":
    print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
ans=[0]*n
ans[0]=a[0]
ans[-1]=a[-1]
for i in range(1,n-1):
    ans[i]=min(a[i],a[i-1])
    
print(sum(ans))

Submission status: Accepted

Solution code:
n=int(input())
B=list(map(int,input().split()))+[10**5]
A=[B[0]]+[0]*(n-1)
for i in range(1,n):
    A[i]=min(B[i-1],B[i])
print(sum(A))

Submission status: Accepted

Solution code:
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(1,n-1):
  a[i]=min(b[i-1],b[i])
ans=sum(a)
print(ans)

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest


def resolve():
    n = int(input())
    b = list(map(int, input().split()))

    a = [0 for _ in range(n)]

    for i in range(n - 1, -1, -1):
        if i == n - 1:
            a[i] = b[i - 1]
        elif i == 0:
            a[i] = b[0]
        else:
            a[i] = min(b[i], b[i - 1])
    print(sum(a))

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3
2 5"""
        output = """9"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """2
3"""
        output = """6"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """6
0 153 10 10 23"""
        output = """53"""
        self.assertIO(input, output)


if __name__ == "__main__":
    # unittest.main()
    resolve()

Submission status: Accepted

Solution code:
n = int(input())
b = list( map(int,input().split()) )

a = [0] * n
c = b[0]
a[0] = b[0]
for i,data in enumerate(b):
  if b[i] >= c:
    a[i+1] = b[i]
  else :
    a[i],a[i+1] = b[i],b[i]
  c = b[i]

print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
b = [int(x) for x in input().split()]

ans = b[0]
for i in range(1, n-1):
    ans += min(b[i-1], b[i])
ans += b[-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

A = [10**5]*N

for i in range(N-1):
    A[i] = min(A[i], B[i])
    A[i+1] = min(A[i+1], B[i])

print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

A = [10**5]*N

for i in range(N-1):
    A[i] = min(A[i], B[i])
    A[i+1] = min(A[i+1], B[i])

print(sum(A))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
ans = [1e9] * int(input())
for i, x in enumerate(map(int, input().split())):
    ans[i] = min(ans[i], x)
    ans[i+1] = min(ans[i+1], x)
print(sum(ans))

Submission status: Accepted

Solution code:
N = int(input())
#6
B = [int(i) for i in input().split()]
# 153 10 10 23

A = [0]*N

A[0] = B[0]
A[N-1] = B[N-2]

for i in range(1,N-1):
	A[i] = min(B[i],B[i-1])

print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = [int(x) for x in input().split()]
A = []

for i in range(N):
    if i == 0:
        A.append(B[i])
    elif i == N-1:
        A.append(B[i-1])
    else:
        A.append(min(B[i-1],B[i]))

print(sum(A))

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
b = list(map(int, input().split()))
b.insert(0, 1000000)
b.append(10000000)
a = 0
for i in range(n):
    a += min(b[i], b[i+1])
print(a)

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))

A=[]
for i in range(n):
    if i==0:
        A.append(b[0])
    elif i==n-1:
        A.append(b[-1])
    else:
        A.append(min(b[i],b[i-1]))
print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))

C = [B[0]]

[C.append(min(B[x],B[x-1])) for x in range(1,N-1)]

C.append(B[N-2])

print(sum(C))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = [int(i) for i in input().split()]

A = [0 for i in range(N)]
A[0] = B[0]
for i in range(N-1):
    A[i] = min(A[i], B[i])
    A[i+1] = B[i]

print(sum(A))

Submission status: Accepted

Solution code:

n = int(raw_input())
b = map(int, raw_input().split(' '))
def f(b):
	n = len(b)+1
	s = 0
	for i in range(n):  s += min(b[i] if i < n-1 else +float('inf'), b[i-1] if i-1>=0 else +float('inf'))
	return s
print f(b)

Submission status: Accepted

Solution code:
# aを1番目の値、2~n-1番目の値、n番目の値と3つに分けて考えてリストに追加
n = int(input())
b = list(map(int, input().split()))

a = []

# aの1番目の値
a.append(b[0])

# aの2~n-1番目の値
b_len = len(b)
for i in range(b_len-1):
    ai = min(b[i], b[i+1])
    a.append(ai)

# aのn番目の値
a.append(b[-1])

print(sum(a))

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit


def main():
    INF = float('inf')
    input = stdin.buffer.readline
    n = int(input())
    b = list(map(int, input().split()))
    b.insert(0, INF)
    b.append(INF)
    a = [0] * n

    for i in range(n):
        a[i] = min(b[i], b[i + 1])

    print(sum(a))


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))

C = [B[0]]

[C.append(min(B[x],B[x-1])) for x in range(1,N-1)]

C.append(B[N-2])

print(sum(C))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

B.append(-1)
b_ = B[0]
ans = 0
for b in B:
    if b < 0:
        ans += b_
    else:
        ans += min(b, b_)
        b_ = b
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
n = int(input())
B = list(map(int, input().split()))

INF = 1001001001
B = [INF]+B+[INF]

ans = 0
for i in range(n):
    ans += min(B[i],B[i+1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

sum = B[0] + B[-1]

for i in range(N-2):
    sum += min(B[i], B[i+1])

print(sum)

Submission status: Accepted

Solution code:
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

N = ri()
B = rl()

A = [B[0]]
for i in range(N-2):
	a = min(B[i], B[i+1])
	A.append(a)
A.append(B[-1])
print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
ans = [-1] * n
ans[0]=b[0]
ans[-1]=b[-1]
for i,B in enumerate(b):
    if i==n-2:
        break
    next_b=b[i+1]
    ans[i+1]=min(B, next_b)
#    print(ans)
    
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

b = tuple(map(int, input().split()))

ans = b[0] + b[n - 2];

for i in range(0, n - 2):
    ans += min(b[i], b[i + 1])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

A = []
for i in range(N):
  if i == 0:
    A.append(B[0])
  elif i == N - 1:
    A.append(B[-1])
  else:
    A.append(min(B[i], B[i - 1]))
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    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)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())

B = [int(x) for x in input().split()]

ans = 0
ans += B[0]

for i in range(1,n-1):
    if(B[i-1] <= B[i]):
        ans += B[i-1]

    else:
        ans += B[i]

ans += B[n-2]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(x) for x in input().split()]
sum=0
n=len(l)
for i in range(0,n):
    if(i==0):
        if(len(l)>1 and l[i]>l[i+1]):
            sum=sum+(l[i]+l[i+1])
        else:
            sum=sum+(l[i]*2)
    elif(i==n-1):
        sum=sum+l[i]
    else:
        if(l[i]<=l[i+1]):
            sum=sum+l[i]
        else:
            sum=sum+l[i+1]
print(sum)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
l = [0] * n
l[0], l[-1] = b[0], b[-1]

for i in range(n-2):
    l[i+1] = min(b[i], b[i+1])

print(sum(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main():
    _ = int(readline())
    Bs = list(map(int, readline().split()))
    As = [Bs[0]] + [min(b, b_next) for b, b_next in zip(Bs, Bs[1:])] + [Bs[-1]]
    print(sum(As))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
lb = list(map(int, input().split()))

ans = lb[0] + lb[len(lb)-1]
for i in range(len(lb)-1):
    ans += min(lb[i], lb[i+1])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
B = [B[0]] + B + [B[-1]]
A = []

for i in range(N):
    A.append(min(B[i], B[i+1]))

print(sum(A))

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
B = deque(map(int, input().split()))
B.append(10 ** 5 + 1)
B.appendleft(10 ** 5 + 1)
ans = 0
for i in range(N):
  ans += min(B[i], B[i + 1])
  
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
B=list(map(int,input().split()))
# print(B)
ans = [[] for i in range(n)]
ans[0] = B[0]
ans[-1]= B[-1]
for i in range(1,n-1):
  ans[i] = [B[i-1],B[i]]
# print(ans)
for i in range(1,n-1):
  ans[i] = min(ans[i])
print(sum(ans))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
ans = 0
INF=2*10**5
old_B=INF
for i in range(N):
	if i==N-1:ans+=old_B
	else:
		ans+=min(old_B, B[i])
		old_B=B[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
stdin = open(0).read().rstrip().split('\n')
N = int(stdin[0])
B = list(map(int,stdin[1].split()))
A = [0]*N
A[0] = B[0]
A[N-1] = B[N-2]
for i in range(1,N-1):
    A[i] = min(B[i],B[i-1])
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
b = [10**9] + list(map(int,input().split())) + [10**9]
a = []

for i in range(N):
  a.append(min(b[i],b[i+1]))

print(sum(a))

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
B = deque(map(int, input().split()))
base = B.popleft()
ans = base
while B:
  tmp = B.popleft()
  ans += min(base, tmp)
  base = tmp
ans += base
print(ans)

Submission status: Accepted

Solution code:
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):
    if A[i+1]<=B[i] and A[i+1]<=B[i+1]:
        A[i+1] = min(B[i], B[i+1])

print(sum(A))

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))
A=[]
A.append(B[0])

for i in range(N-2):
    A.append(min(B[i],B[i+1]))
A.append(B[N-2])
print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))

A = [0] * n
A[0], A[-1] = B[0], B[-1]
for i in range(1, n - 1):
    A[i] = min(B[i], B[i - 1])

print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))

result = b[0] + b[-1]
for i in range(len(b)-1):
    c = [b[i],b[i+1]]
    result += min(c)
if n == 3:
    result = b[0]*2 + b[1]

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
#a,b,x=map(int,input().split())
bl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

al=[0 for i in range(n)]

al[n-1]=bl[-1]

for i in range(n-2,-1,-1):
    al[i]=bl[i]
    if al[i+1]>bl[i]:
        al[i+1]=bl[i]

print(sum(al))

Submission status: Accepted

Solution code:
n=int(input())
b=[int(x) for x in input().split()]
a=[0]*n
for i in range(n-1,-1,-1):
    if i==n-1:
        a[i]=b[i-1]
    elif i==0:
        a[i]=b[i]
    else:
        if b[i]<b[i-1]:
            a[i]=b[i]                                                                                                                                                                                                                                                                                                                           
        else:
            a[i]=b[i-1]
print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))

ans = [0]*n
ans[0] = b[0]
ans[-1] = b[-1]
for i in range(1,n-1):
  ans[i] = min(b[i-1],b[i])
print(sum(ans))

Submission status: Accepted

Solution code:
def maximal_value(n, bli):

    ans = []
    ans.append(bli[0])

    for i in range(n-1):
        if bli[i] < ans[i]:
            ans[i] = bli[i]
        ans.append(bli[i])

    return sum(ans)

def main():
    n = int(input())
    bli = list(map(int, input().split()))
    print(maximal_value(n, bli))

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes) if b else print(no)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx
# from bisect import bisect_left,bisect_right
n=n0()
b=n1()

a=[b[0],b[-1]]
for i in range(1,n-1):
    a.append(min(b[i-1],b[i]))

print(sum(a))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))

A = [0]*N
A[0] = B[0]
A[-1] = B[-1]
for i in range(1,N-1):
    A[i] = min(B[i-1],B[i])

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

b = list(map(int,input().split()))

ans = 0

for i in range(n-2):
    x = b[i]
    y = b[i+1]
    
    ans += min(x,y)
    
print(ans+b[-1]+b[0])

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))

A = [B[0]]
for i in range(1, N):
    if i == N-1:
        A.append(B[-1])
    else:
        A.append(min(B[i-1], B[i]))
print(sum(A))