https://sangsangss.tistory.com/187
n=int(input())
lst = list(map(int, input().split()))
lst.append(0)
lst.append(0)
ans =0
for i in range(len(lst)):
count = lst[i]
if count==0:
continue
if lst[i+1]==0:
ans+=count*3
continue
else:
if lst[i+2]==0:
lst[i+1]-=count
if lst[i+1]<0:
lst[i+1]=0
ans+=count*5
else:
lst[i+1]-=count
lst[i+2]-=count
if lst[i+1]<0:
lst[i+1]=0
if lst[i+2]<0:
lst[i+2]=0
ans+=count*7
print(ans)
It was only right for some test cases because my thought process was like this. I wanted to make the lst[i] to 0 always. But this is wrong.
We do need to sometimes use 2 factories instead of 3 like here