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:
# -*- coding: utf-8 -*-

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

A = []

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

    A.append(B[i])
    if A[i] >= A[i + 1]:
        A[i] = B[i]


print(sum(A))

Submission status: Accepted

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

res = []
res.append(B[0])
res.append(B[0])
for i in range(1, len(B)):
    res[i] = min(res[i], B[i])
    res.append(B[i])

print(sum(res))

Submission status: Accepted

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

A = [0]*N


A[0] = B[0]

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

print(sum(A))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
B = list(map(int, input().split()))
ans = B[0] + B[-1]
Atmp = 0
for i in range(N - 2):
    Atmp = B[i]

    if Atmp > B[i + 1]:
        ans += B[i + 1]
    else:
        ans += Atmp

print(ans)

Submission status: Accepted

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

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

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

#print(N,B)

A=[0]*N

#print(A[0])
A[0]=B[0]
A[N-1]=B[N-2]

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

#print(A)
print(sum(A))

Submission status: Accepted

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


count=0
n=N-1
A=[int(0) for _ in range(N)]
for i in range(0,n):
    A[i]=B[i]

for i in range(0,n):
    if A[i+1]>B[i]:
        A[i+1]=B[i]
        
for i in range(0,n):
    count+=A[i]
    
count+=B[n-1]
print(count)

Submission status: Accepted

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

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

max_A =[B[0]]
B.append(B[N-2])

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

print(sum(max_A))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int,input().split(' ')))
A = []
A.append(B[0])
if N == 2:
  A.append(B[-1])
  print(sum(A))
  exit()
else:
  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:
# -*-coding:utf-8-*-
"""
A:長さNの要素不明の正数列
B:要素bi(1<=i<=N-1)でbiはbi>=max(ai,ai+1)を満たす
Aの要素の総和の最大値を求めよ
要素ai(i>2)はbi-1,biから(両方を超えないように)制約を受ける.その中で最大となるaiを選ぼうと思えば、bi,bi-1の最小値に等しくなる
i==0とi==Nのときは特殊
"""
N=int(input())
B=list(map(int,input().split()))

sum_ai=0
for i in range(N):
    if i==0: # i==0のときbi-1が存在しないので
        min_b = B[i]
        sum_ai += min_b
    elif i<N-1: # i>0
        min_b = min(B[i], B[i-1])
        sum_ai += min_b
    else: # i==N-1
        min_b = B[i-1]
        sum_ai += min_b

print(sum_ai)

Submission status: Accepted

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

A = 0
ans = 0

ans = B[0]

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

ans += B[N-2]

print(ans)

Submission status: Accepted

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


def solve(N: int, B: "List[int]"):

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

            
    
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    B = [ int(next(tokens)) for _ in range(N-1) ]  # type: "List[int]"
    solve(N, B)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
result = a[0] + a[n-2]

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
B = [int(x) for x in input().split()]
A = [10**5] * N
for i, b in enumerate(B):
    A[i] = min(A[i], b)
    A[i+1] = min(A[i+1], b)
print(sum(A))

Submission status: Accepted

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

B.append(B[-1])
B.reverse()

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

A.append(B[-1])
ans = sum(A)

print(ans)

Submission status: Accepted

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

A=[0 for i in range(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]
print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from itertools import tee


def pairwise(iterable):
    x, y = tee(iterable)
    next(y, None)
    return zip(x, y)


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

if len(b) == 1:
    print(2 * b[0])
else:
    s = b[0] + sum(min((i, j)) for i, j in pairwise(b)) + b[-1]
    print(s)

Submission status: Accepted

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

Submission status: Accepted

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

A = [0] * N
sub= [0] * N
ans = 0

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

# A[N-3]まで
for i in range(1, N-2):
    A[i] = min(B[i], B[i-1])

A[0] = B[0]
A[-1] = B[-1] 
A[N-2] = min(B[N-2], B[N-3])

for i in range(N):
    ans = ans + A[i]

print(ans)

Submission status: Accepted

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

cnt = 0 
ss = 0
for i in range(n-1) :
    if(c[i] > b[i]) :
        c[i] = b[i]
    c[i+1] = b[i]

print(sum(c))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

B = list(map(int, input().split()))
b = len(B)
A = []

A.append(B[0])
if len(B) == 1:
    A.append(B[0])
if len(B) > 1:
    for i in range(b-1):
        if B[i] <= B[i +1]:
            A.append(B[i])
        else:
            A.append(B[i+1])
    A.append(B[b-1])
print(sum(A))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

for n in reversed(range(0, len(B))):
  A[n] = B[n]
  A[n+1] = B[n] if B[n] < A[n+1] else A[n+1]  

print(sum(A))

Submission status: Accepted

Solution code:
num = int(input())
B_seq = list(map(int,input().split()))
#print(num)
#print(B_seq)
A_seq=[]
#A_seq[0]=B_seq[0]
for i in range(0,num-1):
    A_seq.append(B_seq[i])
#A_num=seq[num-2]
#print(A_seq)
A_seq.insert(0,B_seq[0])
#print(A_seq)
cnt = 0
for j in range(num-1):
    if max(A_seq[j],A_seq[j+1])<=B_seq[j]:
        pass
    else:
        A_seq[j]=A_seq[j+1]
#print(A_seq)
cnt=0
for x in range(num):
    cnt = cnt+A_seq[x]
print(cnt)

Submission status: Accepted

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

for i in range(n-1):
    a[i+1] = max(a[i],b[i])
    if(i == n-2):
        break
    if(b[i+1]<=b[i]):
        a[i+1] = b[i+1]
print(sum(a))

Submission status: Accepted

Solution code:
N = int(input())

B = [int(n) for n in input().rstrip().split(' ')]
A = []

A.append(B[0])

for i in range(0, N-2):
  if i == 0:
    prev = B[-1]
  else:
    prev = B[0]
  
  next = B[i+1]
  b = B[i]
  
  max_b = (max(prev, b))
  min_b = (min(next, b))
  
  A.append(min_b)

if N > 2:
  A.append(B[-1])
else:
  A.append(B[-1])

print(sum(A))

Submission status: Accepted

Solution code:
N=int(input())

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

sum=0
for i in range(len(B)):
    #print(i)
    if i==0:
        sum+=B[i]
    elif B[i-1]<B[i]:
        sum+=B[i-1]
    else:
        sum+=B[i]
    

sum+=B[len(B)-1]
print(sum)

Submission status: Accepted

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

A =[]
if N == 2:
    print(N*B[0])
    sys.exit()
else:
    if B[1] >= B[0]:
      A.append(B[0])
      A.append(B[0])
    else:
      A.append(B[0])
      A.append(B[1])
    for i in range(1,len(B)-1):
        if B[i+1] >= B[i]:
          A.append(B[i])
        else:
          A.append(B[i+1])
 
x = 0
for y in A:
  x = x + y
print(x+B[-1])

Submission status: Accepted

Solution code:
while True:
    try:
        num = int(input())
        lst = list(map(int, input().split()))
        ans = 0
        ans += lst[-1] + lst[0]
        if num == 2:
            print(ans)
            continue
        for i in range(1, num-1):
            ans += min(lst[i], lst[i-1])
        print(ans)
    except:
        break

Submission status: Accepted

Solution code:
# 2019/09/07
# AtCoder Beginner Contest 140 - C

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

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

sum = sum + b[n-2]

# Output
print(sum)

Submission status: Accepted

Solution code:

n =int(input())
b = list(map(int,input().split()))
a = []
a.append(b[0])
sum = 0


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

if(a[-1]>=b[-1]):
    a.append(a[-1])
else:
    a.append(b[-1])

for j in range(n):
    sum += a[j]

print(sum)

Submission status: Accepted

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

print(sum(A))

Submission status: Accepted

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

if n == 2:
    print(b[0]*2)
elif n==3:
    print(b[1]+min(b[0],b[1])*2)
else:
    co = [min(x , y) for (x, y) in zip(b[0:-1], b[1:])]
    print(sum(co)+b[-1]+b[0])

Submission status: Accepted

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

for i in range(1,N-1):
  c = min([B[i-1],B[i]])
  A.insert(i, c)
  #print(c)
  
A.insert(N-1, B[N-2])

#print(A)
print(sum(A))

Submission status: Accepted

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

S = B[0]
pre_b = -1

for i in range(len(B)):
  val = max(pre_b, B[i])
  if i < N - 2:
    if B[i] > B[i + 1]:
      val = B[i + 1]

  S += val
  pre_b = val
  
print(S)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
ak=l[0]+l[-1]
a=l[0]
if n!=2:
  for i in l[1:]:
    if i<=a:
      ak+=i
    else:ak+=a
    a=i
print(ak)

Submission status: Accepted

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

a=[0]*n
a[0]=b[0]
a[n-1]=b[n-2]
ans=0

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

for j in range(n):
  ans+=int(a[j])

print(ans)

Submission status: Accepted

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

count = 0
for i in range(0, n-1):
    if count == 0:
        a.append(b[i])
        a.append(b[i])
        count = count +1
    elif b[i] > b[i-1]:
        a.append(b[i])
    elif b[i] < b[i-1]:
        del a[i]
        a.append(b[i])
        a.append(b[i])
    elif b[i] == b[i-1]:
        a.append(b[i])
print(sum(a))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(int(1e6))

N = int(input())
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)

Submission status: Accepted

Solution code:
N=int(input())
B= list(map(int,input().split()))
S=0
for i,b in enumerate(B[:-1]):
    if b<B[i+1]:
        S+=b
    else:
        S+=B[i+1]
S += (B[0]+B[-1])
print(S)

Submission status: Accepted

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

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

s = b[0]+b[-1]
p = b[0]


if n > 2:
    for i in range(1,n-1):
        s += min(b[i],p)
        p = b[i]

print(s)

Submission status: Accepted

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

a[0] = b[0]
if n >= 4:
    a[n-1] = b[n-2]
    for i in range(1,n-2):
        if b[i-1] <= b[i] <= b[i+1]:
            a[i] = b[i-1]
            a[i+1] = b[i]
        elif b[i-1] >= b[i] >= b[i+1]:
            a[i] = b[i]
            a[i+1] = b[i+1]
        elif b[i-1] < b[i] > b[i+1]:
            a[i] = b[i-1]
            a[i+1] = b[i+1]
        else:
            a[i] = b[i]
            a[i+1] = b[i]
elif n == 3:
    a[0] = b[0]
    a[1] = min(b[0], b[1])
    a[2] = b[1]
elif n == 2:
    a[0] = b[0]
    a[1] = b[0]

sum = 0
for i in range(n):
    sum += a[i]
print(sum)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int,input().split()))
b.append(1000000) # 末尾に追加
ans = b[0]
for i in range(n-1):
  ans += min(b[i],b[i+1])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().split()))
amt = b[0] + b[n-2]
for i in range(n-1):
    if i != n-2 and b[i] <= b[i+1]:
        amt += b[i]
    elif i != n-2 and b[i] > b[i+1]:
        amt += b[i+1]
print(amt)

Submission status: Accepted

Solution code:
num = int(input())

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

a=[arr[0]]
for i in range(1,num-1):
    a0 = min(arr[i],arr[i-1])

    a.append(a0)
a.append(arr[-1])

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


print(sum(a))

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

n = ni()
b = na()

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

print(ans)

Submission status: Accepted

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

A.append(B[0])

for i in range(1, N - 1):
    if i == N - 1:
        a = B[N - 1]
        print(N, 555)
    else:
        if B[i] > B[i -1]:
            a = B[i -1]
        else:
            a = B[i]
    A.append(a)
    
A.append(B[N-2])

s = 0
for i in A:
    s += i
    
print(s)

Submission status: Accepted

Solution code:
# Aの長さ
N = int(input())
# Bは長さ N-1
B = list(map(int, input().split()))
A=[]

if N == 2:
    print(2*B[0])
else :
    B.append(100001)
    ans = 0
    for i in range(N):
        A.append(min([B[i-1], B[i]]))
        ans += A[i]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
b=list(map(int,input().split()))
a=[0]*n
ans=0
if n == 2:
    print(b[0]*2)
elif n==3:
    print(min(b)*2 + max(b))
else:
    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]
    ans =sum(a)
    print(ans)

Submission status: Accepted

Solution code:
n=int(input())
if n!=2:
    blist=list(map(int,input().split()))
    alist=[blist[0]]
    for i in range(1,n-1):
        alist.append(min(blist[i],blist[i-1]))
    alist.append(blist[n-2])
    gokei=sum(alist)
    print(gokei)
else:
    b=int(input())
    print(int(b*2))

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):
    if len(A) == N :
        A.append(B[-1])
    else :
        A.append(min(B[i:i+2]))
print(sum(A))

Submission status: Accepted

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

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

Submission status: Accepted

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

a = [b[0]]

if len(b) == 2:
    if b[0]<=b[1]:
        print(sum(b)+b[0])
    elif b[0] >b[1]:
        print(sum(b)+b[1])
elif len(b)==1:
    print(b[0]*2)
elif len(b)>=3:
    b.append(0)

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

        elif b[i]>b[i+1]:
            a.append(b[i+1])

    a.append(b[-2])
    print(sum(a))

Submission status: Accepted

Solution code:
_ = input().strip()
b = input().strip().split(" ")

pre_b = int(b[0])
t = 0
for _b in b:
    _b = int(_b)
    t += min(_b, pre_b)
    pre_b = _b
t += _b
print(t)

Submission status: Accepted

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

solution = [0]*n
for i in range(n):
    if i == 0:
        solution[i] = arr[i]
    elif i == n-1:
        solution[i] = arr[i-1]
    else:
        solution[i] = min(arr[i-1], arr[i])

print(sum(solution))

Submission status: Accepted

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

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

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

print(ans)

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 = [int(i) for i in input().split()]

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

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

result = B[N-2]
old = B[N-2]
for i in range(N-3,-1,-1):
    #print(min(B[i],old))
    result = result + min(B[i],old)
    old  = B[i]


result = result + B[0]

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
N, *B = map(int, open(0).read().split())
result = B[0] + B[len(B) - 1]
for i in range(N - 2):
    result += min(B[i], B[i + 1])

print(result)

Submission status: Accepted

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

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

print(Sum + B[0] + B[N-2])

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

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

print(sum(a))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# https://atcoder.jp/contests/abc140/tasks/abc140_b

import sys

N = int(input())
B = list(map(int, input().split()))
A = [B[0]]  # 回答のため作っていくリスト

for idx in range(len(B)):
    current_val = B[idx]
    if idx + 1 == len(B):
        next_val = sys.maxsize
    else:
        next_val = B[idx + 1]

    A.append(min(current_val, next_val))

print(sum(A))

Submission status: Accepted

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

b.insert(n, b[n-2])
b.insert(0, b[0])

a =[]
for i in range(n):
    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()))

ans = B[0]

for i in range(N-2):
    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()))
ans = 0
if n == 2:print(B[0]*2)
else:
  for i in range(n-1):
    if i == 0:
      if B[i] > B[i+1]:
        ans += max(B[i],B[i+1])+min(B[i],B[i+1])
      else:
        ans += min(B[i],B[i+1])*2
    elif i == n-2:
      ans += B[-1]
    else:
      ans += min(B[i],B[i+1])
  print(ans)

Submission status: Accepted

Solution code:
n = int(input())
Bs = list(map(int, input().split()))
Ans = []
for i in range(0, n):
    if i == 0:
        Ans.append(Bs[0])
        continue
    if i == n-1:
        Ans.append(Bs[i-1])
        continue
    Ans.append(min([Bs[i-1], Bs[i]]))
print(sum(Ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
b_s = [int(h) for h in input().split()]

ans = 0
left = b_s[0]
right = left
ans += left
#print(left)
for i in range(1, n - 1):
  right = b_s[i]
  a_i = min(left, right)
  #print(a_i)
  ans += a_i
  left = right
ans += right
#print(right)
print(ans)

Submission status: Accepted

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

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

for i in range(N-2):
    # print(l)
    # print("i=====",i)
    # print("B[i]",B[i])
    if B[i] > B[i+1]:
        l.append(B[i+1])
    else:
        l.append(B[i])
l.append(B[N-2])
# print(l)
print(sum(l))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
hoge = input()
nums = list(map(int, input().split(' ')))[::-1]
current = nums[0]
ans = 0
for n in nums:
    current = min(current, n)
    ans += current
    current = n  # !
print(ans + current)  # !!

Submission status: Accepted

Solution code:
N=int(input())
B = [int(i) for i in input().split()] 
A=[0]*N
A[0]=B[0]
for i in range(N-2):
    A[i+1]=B[i]
    if B[i+1]>=B[i]:
        A[i+2]=B[i+1]
    else:
        A[i+1]=B[i+1]
if B[N-2]<=B[N-3]:
    A[N-1]=B[N-2]
ans=sum(A)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

A = []

A.append(B[0])
A.append(B[0])

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

  else:
    A.pop(i)
    A.append(B[i])
    A.append(B[i])

print(sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

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



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

Submission status: Accepted

Solution code:
N = int(input())
b = list(map(int, input().split()))
a = [ set() for _ in range(N)]
a = [-1]*N
pre = -1
for x in range(N):
    if x==0:
        a[x] = b[x]

    elif x==N-1:
        a[x] = b[x-1]

    else:
        a[x] = min(b[x-1], b[x])

print(sum(a))

Submission status: Accepted

Solution code:
n = int(input())
b = [int(x) for x in 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:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
# スペース区切りの整数の入力
a = map(int, input().split())
alist = list(a)

tmp = []

for i in range(n-1):

    if i == 0:
        tmp.append(alist[i])
        tmp.append(alist[i])
    else:
        tmp.append(alist[i])
        if alist[i] < max(tmp[i], tmp[i+1]):
            tmp[i] = alist[i]


sum = 0
for i in tmp:
    sum += int(i)

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*n
b = [int(i) for i in input().split()]
b.append(10**6)
a[0] = b[0]
for i in range(1,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()))
l = [0] * N
l[0] = B[0]
l[-1] = B[-1]
for i, b1, b2 in zip(range(1, N), B, B[1:]):
        if b1 > b2:
                l[i] = b2
        else:
                l[i] = b1

print(sum(l))

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(a) for a in input().split()]
aaa=[]
if n==2:
  print(a[0]*2)
else:
  aaa.append(a[0])
  for i in range (1,n-1):
    aa = min(a[i-1],a[i])
    aaa.append(aa)
  aaa.append(a[n-2])
  print(sum(aaa))

Submission status: Accepted

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

A_list = []
for i, B in enumerate(B_list):
    if not A_list:
        if len(B_list) > 1 and B > B_list[i+1]:
            A_list.append(B)
            A_list.append(B_list[i+1])
        else:
            A_list.append(B)
            A_list.append(B)
    elif i < len(B_list)-1:
        if B > B_list[i+1]:
            A_list.append(B_list[i+1])
        else:
            A_list.append(B)
    else:
        A_list.append(B)
print(sum(A_list))

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

Submission status: Accepted

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

SUM = 0
A = [0]*N

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

        if len(B) <= 1:
            A[i + 1] = B[i]
        else:
            if B[i] > B[i+1]:
                A[i+1] = B[i+1]
            else:
                A[i+1] = B[i]
        continue

    if i < N-2:
        if B[i] > B[i+1]:
            A[i+1] = B[i+1]
        else:
            A[i+1] = B[i]
    else:
        A[i+1] = B[i]

print(sum(A))

Submission status: Accepted

Solution code:
N=int(input())
B=[10**5]+list(map(int, input().split()))+[10**5]

print(sum(min(B[i], B[i+1]) for i in range(N)))

Submission status: Accepted

Solution code:
N=int(input())
B=list(map(int,input().split()))
total=B[0]
if N==2:
    print(B[0]*2)
    exit()
    
if N==3:
    print(B[0]+min(B[1],B[0])+B[1])
    exit()
for i in range(N-2):
    total+=min(B[i],B[i+1])
    
#if N%2==0:
total+=B[-1]
print(total)

Submission status: Accepted

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