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:
n=int(input())
b=list(map(int,input().split()))
a=list()
a.append(b[0])
for i in range(n-2):
	a.append(min(b[i],b[i+1]))
if not n==1: a.append(b[-1])
print(sum(a))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import accumulate
import sys

def main():
    input = sys.stdin.readline
    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))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = 0
out = 0
for i in range(N-1):
  if count == 0:
    count += 1
    out += B[i]
  else:  
    out += min(B[i-1],B[i])

out += B[N-2]
    
print(out)

Submission status: Accepted

Solution code:
import numpy as np

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

result=0
A=[]
A.append(B[0])
for i in range(len(B)):
    if i!=0:
        A.append(min(B[i],tmp))
    tmp=B[i]
A.append(B[N-2])
result=np.sum(A)
print(result)

Submission status: Accepted

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

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

Submission status: Accepted

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

a = [b[0]]
tmp = 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()))

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

Submission status: Accepted

Solution code:
#C

n =int(input())
b = list(map(int,input().split()))
b.insert(0,10 ** 5)
b.append(10 ** 5)
a=[]

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

Submission status: Accepted

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

ans = 0
for i in range(1, n-1):
	if b[i] >= b[i-1]:
		ans += b[i-1]
	else:
		ans += b[i]

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
#N,A,B = map(int,input().split())
B_list = list(map(int,input().split()))
#X1_list = [int(input()) for i in range(N)]
#X2_list = [list(map(int,input().split())) for i in range(N)]
target =  [0] * N
target[0] = B_list[0]
target[1] = B_list[0]
for i in range(1,N-1):
    #print(target)
    if B_list[i-1] > B_list[i]:
        target[i] = B_list[i]
        target[i+1] = B_list[i]
    else:
        target[i+1] = max(B_list[i-1],B_list[i])
#print(target)
print(sum(target))

Submission status: Accepted

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

Submission status: Accepted

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

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[i])
  elif i == (n - 1):
    a.append(b[i-1])
  else:
    a.append(min(b[i], b[i-1]))
print(sum(a))

Submission status: Accepted

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

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

print(sum(arr1))

Submission status: Accepted

Solution code:
N = int(input())
B_list = list(int(Bi) for Bi in input().split(' '))

num=0
B_pre=0

for i in range(len(B_list)):
  if i==0:
    num+=B_list[i]
    B_pre=B_list[i]
  else:
    if B_list[i]>=B_pre:
      num+=B_pre
    else:
      num+=B_list[i]
    
    B_pre=B_list[i]
    
num+=B_list[-1]

print(num)

Submission status: Accepted

Solution code:
import sys

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

a = [0] * N
a[0] = b[0]
for i in range(len(b) - 1):
    if b[i] > b[i + 1]:
        a[i + 1] = b[i + 1]
    else:
        a[i + 1] = b[i]
a[-1] = b[-1]

print(sum(a))

Submission status: Accepted

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

a = []
a.append(b[-1])
for i in range(1,N-1):
    a.append(min(b[-i], b[-(i+1)]))
a.append(max(b[0], a[N-2]))

print(sum(a))

Submission status: Accepted

Solution code:
''' بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ '''
#codeforces
gi = lambda : list(map(int,input().split()))
n, = gi()
b = gi()
a = [1e9] * n
k = 0
while k < n - 1:
	a[k] = min(a[k], b[k])
	a[k + 1] = min(a[k + 1], b[k])
	k += 1
print(sum(a))

Submission status: Accepted

Solution code:
N = int(input())
B = [int(_) for _ in input().split()]
ans = [0] * N
ans[0] = B[0]
ans[N - 1] = B[len(B) - 1]

for i in reversed(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()))

A = 0
for idx, b in enumerate(B):
  if idx == 0:
    A += b
  else:
    A += min(B[idx], B[idx-1])
A +=  B[-1]
print(A)

Submission status: Accepted

Solution code:
n = int(input())
b_list = list(map(lambda x: int(x), input().split()))
a_list = [0] * n

for i in range(n):
    if i == 0:
        a_list[i] += b_list[0]
    elif i == n-1:
        a_list[i] += b_list[n-2]
    else:
        a_list[i] += min(b_list[i-1], b_list[i])
            
ans = sum(a_list)
print(ans)

Submission status: Accepted

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

if N ==  2:
  print(B[0]*2)

else:
  for i,v in enumerate(B):
    if i != N-2:
      if B[i] <= B[i+1]:
        if A[i] == 'a':
          A[i] = B[i]
        if A[i+1] == 'a':
          A[i+1] = B[i]
      else:
        if A[i] == 'a':
          A[i] = B[i]
    else:
      if A[i] == 'a':
        A[i] = B[i]
      if A[i+1] == 'a':
        A[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[0]

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

print(sum(A))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
b = [100001] + [int(i) for i in input().split()] + [100001]
a=[]
for i in range(n):
    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()))

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

print(m)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

import sys

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    n = int(lines[0])
    b = [int(e) for e in lines[1].split(" ")]

    return (n, b)


def solve(n, b):

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

    a.append(b[n-2])

    return sum(a)


def main():
    # 出力
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r)
    else:
        print("%s" % result)


if __name__ == '__main__':

    main()

Submission status: Accepted

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

for i in range(N-2):
    if B[i] >= B[i+1]:
        total += B[i+1]
    else:
        total += B[i]
total += B[N-2]
print(total)

Submission status: Accepted

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

if n == 1:
        print(b[0] * 2)

else:
        a += b[0]
        bb = b[0]
        for i in range(1,n-1):
                if b[i] > bb:
                        a += b[i-1]
                else:
                        a += b[i]
                bb = b[i]
        a += b[n - 2]
        print(a)

Submission status: Accepted

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

    Ans=B[0]

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

    Ans+=B[N-2]

    print(Ans)

main()

Submission status: Accepted

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

for i in range(n-1):
  a_1 = a[i]
  a_2 = b[i]
  if b[i] >= max(a_1,a_2):
    a.append(b[i])
  else:
    a.append(b[i])
    a[i] = b[i]

print(sum(a))

Submission status: Accepted

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

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

print(s)

Submission status: Accepted

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

a = [b[0], b[n-2]]

for i in range(n-2) :
  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()))
a = [0] * n
for i in range(1,n-1):
         a[i] = min(b[i],b[i-1])
a[0] = b[0]
a[n-1] = b[n-2]
print(sum(a))

Submission status: Accepted

Solution code:

n = int(input())

blist =list(map(int,input().split()))
a = blist[0]+blist[-1]

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

print(a)

Submission status: Accepted

Solution code:
N=int(input())
B = list(map(int, input().split()))
A = [0 for i in range(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 = 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], B[i-1])

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
B = list(map(int,input().split()))
sum = 0

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

Submission status: Accepted

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

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

print(score)

Submission status: Accepted

Solution code:
# C - Maximal Value
N = int(input())
B = list(map(int, input().split()))

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

print(maxA)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
'use strict'
N = int(input())
B = list(map(int, input().split()))
A = [0 for i in range(N)]

A[0] = B[0]
A[1] = B[0]

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

print(sum(A))

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

if n == 2:
  a.append(b[0])
  a.append(b[0])
else:
  for i in range(n-1):
    if i == 0:
      if b[0] >= b[1]:
        a.append(b[0])
        a.append(b[1])
      else:
        a.append(b[0])
        a.append(b[0])
    elif i == n-2:
      a.append(b[i])
    else:
      if b[i] >= b[i+1]:
        a.append(b[i+1])
      else:
        a.append(b[i])

print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split(" ")))
A = B[0]
An = B[0]
C = B[0]
for i in range(1,n-1):
  if An <= B[i]:
    C += An
  else:
    C+= B[i]
  An = B[i]

C += An
print(C)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(lambda x:int(x),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:
        if b[i-1]<b[i]:
            a.append(b[i-1])
        else:
            a.append(b[i])


#print(a)


def sum(list):
    lsum = 0
    for i in range(len(list)):
        lsum = lsum+list[i]

    return lsum

print(sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np


N = int(input())
B = np.array(input().split(), np.int32)
A = np.zeros_like(np.arange(N, dtype='int'))

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()))

score = b[0]+b[-1]

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(n-2):
    ans+= min(a[i],a[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(N - 2):
  A.append(min(B[i], B[i + 1]))
A.append(B[N - 2])
print(sum(A))

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
b = np.array(list(map(int,input().split())))
b = b[::-1]
a = np.array([])

a = np.append(a,b[0])

for index in range(len(b)-1):
  if b[index+1]<b[index]:
    a = np.append(a,b[index+1])
  else:
    a = np.append(a,b[index])

a = np.append(a,b[n-2])

print(int(np.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):
    if(b[i-1]>=b[i]):
        a += b[i]
    else:
        a += b[i-1]
a+=b[n-2]
print(a)

Submission status: Accepted

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

a = []
buf = -1
for i in b :
    a.append(max(buf, i))
    if buf == -1 :
        a.append(i)
    buf = i

for j in range(n - 1):
    if b[j] < a[j] and b[j] < a[j + 1]:
        a[j] = b[j]
        a[j + 1] = b[j]
print(sum(a))

Submission status: Accepted

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

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

Submission status: Accepted

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

print(count)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
B=list(map(int,input().split()))
A=[B[0]]

for i in range(N-1):
    if B[i]<A[i]:
        A[i]=B[i]
    A.append(B[i])

print(sum(A))

Submission status: Accepted

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

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

print(sum(a))

Submission status: Accepted

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

import numpy as np

A = np.zeros(N)
A[N-1] = B[N-2] 

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

print(int(np.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 = []
A.append(B[N-2])
for i in reversed(range(1, len(B))):
    if B[i]>=B[i-1]:
        A.append(B[i-1])
    else:
        A.append(B[i])

A.append(B[0])

print(sum(A))

Submission status: Accepted

Solution code:
n = int(input())
B = list(map(int, input().split()))
A = []*n
ans = B[0]
curosr = B[0]
for b in B[1:]:
  if curosr < b:
    ans += curosr
  else:
    ans += b
  curosr = b
ans +=B[-1]
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(n):
  if i ==0:
    ans += nums[0]
  else:
    if i+1<=len(nums):
      ans += min(nums[i],nums[i-1])
    else:
      ans += nums[-1]
print(ans)

Submission status: Accepted

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

sum = B[0]+B[N-2]

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 for _ in range(N)]
A[1] = A[0] = B[0]
for i in range(1,N-1):
    #print("i",i)
    A[i] = min(B[i], B[i-1])
    #A[i+1] = B[i]
    #print("Ai",A[i])
A[N-1] = B[N-2]
print(sum(A))

Submission status: Accepted

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

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()))+[100001]

a=[0]*n
a[0]=b[0]
bi1=b[0]

for i in range(1,n):
    bi=b[i]
    a[i]=min(bi,bi1)
    bi1=bi
s=sum(a)
print(s)

Submission status: Accepted

Solution code:
def solve():
  import sys
  import math

  sys.setrecursionlimit(10**7)
  readline=sys.stdin.readline

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

  print(a)


if __name__ == '__main__':
  solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
a = []
c = 10**5 #Bn-1

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

print(sum(a)+b[n-2])

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from heapq import *
from itertools import *
from math import *
from operator import *
import numpy as np

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N = int(input())
    BS = list(map(int, input().split()))
    ans = 0
    prev = float('inf')
    for i in range(N-2,-1,-1):
        if BS[i] <= prev:
            ans += BS[i]
        else:
            ans += prev
        prev = BS[i]
    ans += BS[0]
    
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
B = list(map(int, input().split()))

sum_ = 0
B.insert(0, B[0])
for b1, b2 in zip(B[1:], B[:-1]):
  sum_ += min(b1, b2)

print(sum_ + B[-1])

Submission status: Accepted

Solution code:
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy

# If you use recursive call, uncomment this code
#sys.setrecursionlimit(10**6)

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())

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

sum = B[0] + B[N-2]

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()))
sum = 0

for i in range(len(B)+1):   
  if i == 0:
    sum += B[0]
  elif i > 0 and i < N-1:
    sum += min(B[i-1],B[i])
  else:
    sum += B[N-2]
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

ans.append(B[len(B) - 1])

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

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from copy import deepcopy as dp
N = int(input())
B = [int(i) for i in input().split()]
ans = dp(B)
ans.insert(0,B[0])
cnt = 0
while cnt < len(B):
    for i in range(len(B)):
        if max(ans[i],ans[i+1]) <= B[i]:
            cnt+=1
        else:
            cnt = 0
            if ans[i] < B[i]:
                ans[i+1] = B[i]
            else:
                ans[i] = B[i] 
print(sum(ans))

Submission status: Accepted

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

A = [0] * N
# min_index = B.index(min(B))

A[0], A[1] = B[0], B[0]
for i in range(N - 1):
    if B[i] < A[i]: A[i] = B[i]
    A[i + 1] = B[i]
print(sum(A))

Submission status: Accepted

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

A = [0 for i in range(N)]
A[N-1] = B[N-2]
for i in reversed(range(1,N-1)):
    if B[i-1] <= B[i] :
        A[i] = B[i-1]
    else:
        A[i] = B[i]
A[0] = B[0]
print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))
B.append(B[len(B)-1])

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

print(cnt + B[N-2])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python
# coding: UTF-8

from math import *
import os
import sys

#import scipy.optimize
import numpy as np


##################################################################
if __name__ == "__main__":
    Input = [input() for i in range(2)]
    N = int(Input[0])
    B = list(map(int,Input[1].split()))
    B.reverse()

    A = [1+1E5]*N
    for i in range(N-1):
        if B[i] <= A[i]:
            A[i] = B[i]
        A[i+1] = B[i]
    print(sum(A))

Submission status: Accepted

Solution code:
from sys import stdin

def main():
    N = int(input())
    B = list(map(int, input().split()))

    maxb = max(B)
    ans = [maxb] * N
    for i, b in enumerate(B):
        ans[i] = min(ans[i], b)
        ans[i+1] = min(ans[i+1], b)
    print(sum(ans))

input = lambda: stdin.readline()[:-1]
main()

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:i+2])
ans += B[n-2]

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

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

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
mod = 10**9 + 7

import numpy as np

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
b = na()
ans = b[0] + b[-1]
bpre = b[0]
for bi in b[1:]:

        if bpre < bi:
                ans += bpre
        else:
                ans += bi
        bpre = bi
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

nA = [ 0 for i in range(N)]
nB = [ 0 for i in range(N)]

Tmp = []
Tmp = input().rstrip().split(' ')

for i in range(N-1):
  nB[i] = int(Tmp[i])

Ans = nB[0]
Ans += nB[N-2]

for i in range(N-2):
  B1 = nB[i]
  B2 = nB[i+1]
  Tmp = min(B1, B2)
  Ans += Tmp

print(Ans)

Submission status: Accepted

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

l = [b[0]]
if n>=3:
  for i in range(0, len(b)-1):
      test =  b[i+1] - b[i]
      if test>=0:
          l.append(b[i])
      else:
          l.append(b[i+1])
  l.append(b[-1])
  print(sum(l))
else:
  print(b[0]*2)

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
B.append(B[N-2]+1)
S = B[0]
for i in range(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()))
ans = 0
for i in range(n):
  if i == 0:
    ans += B[i]
  elif i == n-1:
    ans += B[-1]
  else:
    ans += min(B[i],B[i-1])
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]
sum = a[0] + a[n-1]
for i in range(1,n-1):
    a[i] = min(b[i-1],b[i])
    sum += a[i]
print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
#al=list(map(int,input().split()))
bl=list(map(int,input().split()))	
#cl=list(map(int,input().split()))	

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


al[0]=bl[0]
al[1]=bl[0]

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split()))
A = []
for i in range(N-2):
    A.append(min(B[i],B[i+1]))
A.append(B[0])
A.append(B[N-2])
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-1):
    a.append(b[i])

    if(a[i] > b[i]):
        a[i] = b[i]

#print(b)
print(sum(a))

Submission status: Accepted

Solution code:
import sys
inputs = sys.stdin.readlines()

N = int(inputs[0])
B = list(map(int, inputs[1].split()))

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

Submission status: Accepted

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

ans = [0]*n
ans[n-1] = b[-1]
for j in reversed(range(n-1)):
  b_ = b[j]
  if ans[j+1] > b_:
    ans[j+1] = b_
  ans[j] = b_
#print(ans)  
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = B[-1]

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

ans += B[0]
print(ans)