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:
#!/usr/bin/env python3
import sys


def solve(N: int, B: "List[int]"):
    B.append(10000000)
    result = 0
    for i in range(N):
        j = N - i - 1
        # 左から
        result += min(B[j], B[j-1])
    print(result)


# Generated by 1.1.5 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())
B = list(map(int, input().split()))

if N == 2:
    ans = 2*B[0]
else:
    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()))

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]

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

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

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[0]
ans += b[-1]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
*B, = map(int, input().split())
MAX = 10**10
A = [MAX] * N
A[0] = B[0]
for i, b in enumerate(B):
    if A[i] > b: A[i] = b
    if A[i+1] > b: A[i+1] = b
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):
    if B[i-1] > B[i]:
        A.append(B[i])
    else:
        A.append(B[i-1])
A.append(B[-1])
print(sum(A))

Submission status: Accepted

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

if n==1:
    print(b[0]*2)
else:
    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:
import math
import queue

mod = 1e9+7

def swap(a,b):
    return (b,a)

def gcd(a,b):
    if (a<b):
        a,b = swap(a,b)
    if (b==0):
        return a
    else:
        return gcd(b,a%b)

def divisors(a):
    divisors = []
    for i in range(1,int(a**0.5)+1):
        if a%i==0:
            divisors.append(i)
            if i!=a//i:
                divisors.append(a//i)
    return divisors

def main():
    n = int(input())
    b = [x for x in map(int,input().split())]
    ans = [b[0]]
    for i in range(n-1):
        if ans[i]>b[i]:
            ans[i] = b[i]
        ans.append(b[i])
    print(sum(ans))
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
prev = 10 ** 5
for b in B:
    ans += min(prev, b)
    prev = b

ans += B[-1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
b = list(map(int, input().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()))
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 = B[0] + B[-1]
for i, j in zip(B, B[1:]):
    ans += min(i, j)

print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
sys.setrecursionlimit(4100000)

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

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
B.append(10**6)

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(s) for s in raw_input().split(" ")][0]
B = [int(s) for s in raw_input().split(" ")]
res = B[0]
for i in xrange(N-2):
    res += min(B[i], B[i+1])
res += B[-1]
print(res)

Submission status: Accepted

Solution code:
N = int(input())
B = [int(x) for x in input().split()]
ans = B[0] + B[-1]
for i in range(1,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()))
ans = [0] * n
ans[0] = b[0]
ans[1] = b[0]
for i in range(1, n - 1):
    ans[i] = min(ans[i], b[i])
    ans[i + 1] = b[i]
print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()

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

Submission status: Accepted

Solution code:
N = int(input())
#S = list(input())

#N,K=map(int,input().split())
#A=list(map(int,input().split()))
B=list(map(int,input().split()))
#C=list(map(int,input().split()))

ans=0

A=[max(B) for i in range(N)]

for i,b in enumerate(B):
    if A[i] > b:
        A[i]=b
    if A[i+1] > b:
        A[i+1]=b

ans=sum(A)



print(str(ans))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
from math import factorial, ceil, floor
from bisect import bisect_right as bsr
from operator import itemgetter as ig
from collections import defaultdict as dd
from collections import deque, Counter as cnt

# お約束
args = None
INF = float("inf")
MOD = int(1e9 + 7)
def input(*ps):
    if type(ps[0]) is list:
        return [input(*ps[0][:-1]) for _ in range(ps[0][-1])]
    elif len(ps) == 1:
        return ps[0](next(args))
    else:
        return [p(next(args)) for p in ps]
def nlist(n, v):
    if not n: return [] if type(v) is list else v
    return [nlist(n[1:], v) for _ in range(n[0])]

# エントリーポイント
def main():
    N = input(int)
    B = input([int, N - 1])

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

    return

if __name__ == '__main__':
    args = iter(sys.stdin.read().split())
    main()

Submission status: Accepted

Solution code:
n = int(input())
b_list = [ int(v) for v in input().split() ]

a_list = [None for i in range(n)]
for i in range(n):
    if i == 0:
        a_list[0] = b_list[0]
    elif i == n - 1: 
        a_list[-1] = b_list[-1]
    else:
        a_list[i] = min(b_list[i-1], b_list[i])

print(sum(a_list))

Submission status: Accepted

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

A = [0] * N
for i in range(N):
    Ai_tmp = 10 ** 18
    if i > 0:
        Ai_tmp = min(Ai_tmp, B[i - 1])
    if i < (N - 1):
        Ai_tmp = min(Ai_tmp, B[i])
    A[i] = Ai_tmp

print(sum(A))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
ans = 0
ans += B[0]
ans += 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()))
INF=10**6
c=[INF]+b+[INF]

a=[0]*n

for i in range(n):
  a[i]=min(c[i],c[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(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 = [int(x) for x in input().split()]
B.append(10**6)
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()))

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

print(sum(a))

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    B = list(map(int, input().split()))
    
    S = 0
    
    for i in range(N):
        if i == 0:
            S += B[i]
        elif i == N-1:
            S += B[i-1]
        else:
            S += min(B[i], B[i-1])
        
    print(S)

Submission status: Accepted

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

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(j) for j in input().split()]
ans=b[0]+b[-1]
for i in range(n-2):
    ans+=min(b[i],b[i+1])
print(ans)

Submission status: Accepted

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

#B_i = max(A_i, A_(i+1))の時が総和最大になる.
#B_(i-1) = max(A_(i-1), A_i)より、max(A_i) = min(B_i, B_(i-1)).
# ただし、B_-1=inf, B_n=infとしておく
a_list_sum = b_list[0]
for i in range(1, n-1):
    a_list_sum += min(b_list[i], b_list[i-1])
a_list_sum += b_list[n-2]
print(a_list_sum)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
b = [int(i) for i in input().split(" ")]
ar = np.c_[[10**5]+b, b+[10**5]]
print(np.sum(np.min(ar, axis=1)))

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):
    if b[i]<b[i+1]:
        ans+=b[i]
    else:
        ans+=b[i+1]

print(ans)

Submission status: Accepted

Solution code:
N, *B = map(int, open(0).read().split())

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
da=list(map(int,input().split()))
da.insert(0,1000000)
da.append(10000000)
ans=0
for i in range(n):
    ans+=min(da[i],da[i+1])
print(ans)

Submission status: Accepted

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


def solve(N: int, B: "List[int]"):
    sm = 0
    for i in range(1, N-1):
        sm += min(B[i-1], B[i])
    sm += B[0] + B[-1]
    print(sm)
    return


# Generated by 1.1.5 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())
b = list(map(int,input().split()))
a=[0 for _ in range(n)]

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)

def main():
    n=int(input())
    bb=list(map(int, input().split()))
    a=[0]*n
    ans=0
    for i,(b0,b1) in enumerate(zip(bb,bb[1:])):
        ans+=min(b0,b1)
    ans+=bb[0]+bb[-1]
    print(ans)

main()

Submission status: Accepted

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

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

ans += B[0]
print (ans)

Submission status: Accepted

Solution code:

N=int(input())

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
n = int(input())
b_ls = [int(i) for i in sys.stdin.readline().split()]
a_ls = [0 for i in range(n)]
a_ls[-1] = b_ls[-1]
for i in range(n-2,-1,-1):
    if i == 0:
        a_ls[0] = b_ls[0]
    else:
        a_ls[i] = min(b_ls[i], b_ls[i-1])
print(sum(a_ls))

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
for i, e in enumerate(b):
    a[i] = min(a[i], e)
    a[i + 1] = min(a[i + 1], e)

print(sum(a))

Submission status: Accepted

Solution code:
N = int(input())
B = list(map(int, input().split()))
 
inf = 10**10
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()))

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

print(sum(A))

Submission status: Accepted

Solution code:
n = int(raw_input())
b = map(int,raw_input().split())
a = []
a.append(b[0])
a.append(b[0])
for i in xrange(1,n-1):
	if b[i] < a[i]:
		a[i] = b[i]
	a.append(b[i])
#print a
print sum(a)

Submission status: Accepted

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

a_list = [b_list[0]]

for i in range(N - 1):
	b = b_list[i]
	if a_list[i] > b:
		a_list[i] = b
	a_list.append(b)
print(sum(a_list))

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 = list(map(int, input().split(" ")))

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

print(out)

Submission status: Accepted

Solution code:

N = int(input())

B = list(map(int,input().split()))
B.append(float("inf"))

for i in range(N):

    if i == 0:
        ans = B[0]

    else:
        ans += min(B[i-1],B[i])

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(len(B)-1):
    ans += min(B[i], B[i+1])
print(ans)

Submission status: Accepted

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

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=[int(x) for x in input().split()]
print(B[0] + sum(min(B[i-1], B[i]) for i in range(1, N-1)) + B[N-2])

Submission status: Accepted

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

N = int(input())
B = list(map(int, 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:
def solve(b, n):
	a = [10**10 for _ in range(n)]

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

	return sum(a)

def main():
	n = int(input())

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

	print (solve(b,n))

if __name__ == "__main__":
	main()

Submission status: Accepted

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

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)