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: Runtime Error

Solution code:
import copy
n = int(input())
b = list(map(int,input().split()))
ans = 0
ls = copy.copy(b)

for i in range(n-1):
    if i > (n-2):
        break
    if b[i] == 0:
        ls.remove(0)
        ans -= b[i+1]
    else:
        ans += b[i]

if n == 2:
    print(min(b)*2)
else:
    print(ans+min(ls))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
num = int(input())
array = list(map(int, input().split()))
sum = 0
 
for (i = 0; i < num; i++):
	if i == 0:
      sum += array[i]
    else if i < (num-1):
      if array[i] > array[i-1]:
        sum += array[i]
      else:
        sum += array[i-1]
    else:
      sum += array[i-1]
        
print(sum)

Submission status: Runtime Error

Solution code:
import sys


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


def main():
    N = int(input())
    B = [int(i) for i in input().split()]
    b = B.copy()
    for i in (range(N-3, -1, -1)):
        if B[i] > B[i+1]:
            B[i] = B[i+1]

    for i in range(N-2):
        if b[i] < b[i+1]:
            b[i+1] = b[i]
    ans1 = sum(B)+B[0]
    ans2 = sum(b)+b[len(b)-1]
    print(ans1 if ans1 >= ans2 else ans2)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int N,i;
   long long sum=0, *B;
   B=(long long*)malloc((N-1)*sizeof(long long));
   cin>>N;
   for(i=0;i<(N-1);++i)
   {
      cin>>B[i];
   }
   for(i=0;i<(N-1);++i)
   {
      sum+=B[i];
   }
   cout<<(B[0]+sum);
   return 0;

}

Submission status: Runtime Error

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

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

ans += B[N-2]

print(ans)

Submission status: Runtime Error

Solution code:
num = int(input())
array = map(int, input().split())
sum = 0
 
for (i = 0; i < num; i++):
	if i == 0:
      sum += array[i]
    else if i < (num-1):
      if array[i] > array[i-1]:
        sum += array[i]
      else:
        sum += array[i-1]
    else:
      sum += array[i-1]
        
print(sum)

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;

int main()
{
   int N,i,sum=0,*B;
   B=(int*)malloc((N-1)*sizeof(int));
   cin>>N;
   for(i=0;i<(N-1);++i)
   {
      cin>>B[i];
   }
   for(i=0;i<(N-1);++i)
   {
      sum+=B[i];
   }
   cout<<(B[0]+sum);
   return 0;

}

Submission status: Runtime Error

Solution code:
n = int(input())

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

Submission status: Runtime Error

Solution code:
N = int(input())
B = list(map(int,input().split()))
ins = 0
count = 0
for i in range(N):
  if i == 0:
    count += B[i]
  if i > 0 and B[i-1] > B[i]:
    count += B[i]
  elif i > 0 and B[i-1] <= B[i]:
    count += B[i-1]
print(count)

Submission status: Runtime Error

Solution code:
num = int(input())
array = map(int, input().split())
sum = 0

for (i = 0; i < num; i++):
	if i == 0:
      sum += i
    else if i < (num-1):
      if array[i] > array[i-1]:
        sum += array[i]
      else:
        sum += array[i-1]
    else:
      sum += array[i-1]
        
print(sum)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    a = [int(i) for i in input().split()]
    b = [int(i) for i in input().split()]
    c = [int(i) for i in input().split()]
    ans = 0
    pre = -1
    for i in range(n):
        ans += b[a[i] - 1]
        if pre == a[i] - 1:
            ans += c[pre - 1]
        pre = a[i]
    print(ans)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n=int(input())
b = list(map(int,input().split()))
b.append(1000000)
li = []
for i in range(n-1):
    if((b[i]>0) and b[i] <= b[i+1] ):
        li.append(b[i])

print(sum(li)+min(li))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
bs = list(map(int, input().split()))
arr = []
for i in range(n):
    if i == 0 or i == 1:
        arr.append(bs[0])
    else:
        arr.append(bs[i-1])
        if bs[i-1] < arr[i-1]:
            arr[i-1] = bs[i]
print(sum(arr))

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize ("-ffloat-store")  
#pragma GCC optimize ("-fno-defer-pop")
typedef long long int ll; 
typedef long double ld; 


	
int main()
{
	std::ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	
	ll n;
	cin>>n;
	
	ll br[n+1];
	
	for(ll i=0;1+i<n;i++)
	{
		cin>>br[i];
	}
	ll ar[n+1];
	memset(ar,-1,sizeof(ar));
	for(ll i=0;i<n-1;i++)
	{
		
		if(i!=n-2)
		{
			
			if(br[i+1]>=br[i])
			{
				if(ar[i]==-1)
				ar[i] = max(ar[i],br[i]);
				ar[i+1] = max(ar[i+1],br[i]);
			}
			else{
				if(ar[i]==-1)
				ar[i] = max(ar[i],br[i]);
			}
		}
		else{
			if(ar[i]==-1)
			ar[i] = max(ar[i],br[i]);
			ar[i+1] = max(ar[i+1],br[i]);
		}
		
	}
	ll ans = 0;
	for(ll i=0;i<n;i++)
	{
		//cout<<ar[i]<<" ";
		ans+=ar[i];
	}
	
	cout<<ans<<endl;
}

Submission status: Runtime Error

Solution code:
N=int(input())
B=[]
for i in range(N-1):
    x=int(input())
    B.append(x)
print(B[0]+sum(B))

Submission status: Runtime Error

Solution code:

A = [0]*(len(B)+1)

A[0] = B[0]
for i in range(len(B)):
    if A[i]>=B[i]:
        A[i]=B[i]
    A[i+1]=B[i]
print(sum(A))

Submission status: Runtime Error

Solution code:
# Author : prayushd
# Language : Python

import re
import os
import sys
from math import ceil, inf
from fractions import gcd
from queue import Queue
from collections import defaultdict, Counter, deque
from bisect import bisect, insort, bisect_left, bisect_right

mod = int(1e9)+7
input = sys.stdin.readline

'''
Check for special cases (n=1)
One wrong submission = 10 mins penalty!
do smth instead of nothing and stay organized
'''

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	ans = [inf]*n
	for i in range(n-1):
		ans[i] = min(ans[i], arr[i])
		ans[i+1] = min(ans[i+1], arr[i])
	# print(ans)
	print(sum(ans))


if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
As = []

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

print(sum(As))

Submission status: Runtime Error

Solution code:
N = int(input())
foods = [int(i) for i in input().split()]
eff = sum([int(i) for i in input().split()])
plus = [int(i) for i in input().split()]
if foods[-1]-2 in plus:
  print(eff + plus[foods[-1]-2])
else:
  print(eff)

Submission status: Runtime Error

Solution code:
# Author : prayushd
# Language : Python

import re
import os
import sys
from math import ceil
from fractions import gcd
from queue import Queue
from collections import defaultdict, Counter, deque
from bisect import bisect, insort, bisect_left, bisect_right

mod = int(1e9)+7
input = sys.stdin.readline

'''
Check for special cases (n=1)
One wrong submission = 10 mins penalty!
do smth instead of nothing and stay organized
'''

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	ans = [inf]*n
	for i in range(n-1):
		ans[i] = min(ans[i], arr[i])
		ans[i+1] = min(ans[i+1], arr[i])
	print(sum(ans))


if __name__ == '__main__':
	main()

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
b = 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: Runtime Error

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

Submission status: Runtime Error

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