Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

パンケーキ

あなたが勤めているパンケーキ屋では、細長い鉄板にパンケーキの生地を横１列に並べて焼きます。パンケーキはへらで何回か裏返せば完成します。何回以上裏返せば完成するかはパンケーキごとに異なります。

へらは大きいので、隣り合ったパンケーキは２枚同時に裏返されてしまいます。このとき、これら２枚の位置は入れ替わりません。ただし、両端だけは、隣のパンケーキといっしょに裏返すだけでなく、１枚だけ裏返すこともできます。すべてのパンケーキを必要な回数以上裏返したら、全部いっぺんに鉄板からおろして完成です。

パンケーキを必要な回数より多く裏返すと固くなってしまうので、あまり多く裏返したくありません。そこであなたは、すべて完成するまでに、各パンケーキが裏返る回数の総和が最小になるような方法を見つけようと考えました。

鉄板の上のパンケーキの枚数と、完成するまでに何回以上裏返さなければならないかがパンケーキごとに与えられているとき、すべて完成するまでに各パンケーキが裏返る回数（へらを操作する回数ではない）の総和の最小値を計算するプログラムを作成せよ。

Input

入力は以下の形式で与えられる。 

N
p1 p2 ... pN

１行目にパンケーキの枚数 N (3 ≤ N ≤ 5000)が与えられる。２行目に各パンケーキが完成するまでに必要な裏返す回数 pi (0 ≤ pi ≤ 3)が与えられる。

Output

すべて完成するまでに各パンケーキが裏返る回数の総和の最小値を１行に出力する。 

Sample Input 1

3
1 2 1

Sample Output 1

4

へらを１回操作して左端と真ん中のパンケーキを裏返すと、２個のパンケーキが１回ずつ裏返るので、この操作で裏返る回数は２回。さらに、へらを１回操作して真ん中と右端のパンケーキを裏返すと、２個のパンケーキが１回ずつ裏返るので、この操作で裏返る回数は２回。以上の総和４回が答えになる。

Sample Input 2

3
0 3 0

Sample Output 2

6

へらを１回操作して左端と真ん中のパンケーキを裏返すと、この操作で裏返る回数は２回。これを３回繰り返したときの、総和の６回が答えになる（真ん中のパンケーキは、両隣のどちらかのパンケーキと一緒に裏返すことしかできないことに注意）。



Sample input:
3
1 2 1


Sample output:
4

Submission status: Wrong Answer

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

P=P+[0]

cnt=0
for i in range(N):
    cnt+=P[i]*2
    P[i+1]=max(0,P[i+1]-P[i])
    #print(P)
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(0,N-1,1):
    if p[i]>=0:
        if i==0 and p[1]==0:
            ans += p[i]
            continue
        ans+=p[i]*2
        p[i+1]-=p[i]
if p[N-1]>=0:
    ans+=p[N-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(0,N-1,1):
    if p[i]>=0:
        if i==0 and p[0]>p[1]:
            ans += (p[0]-p[1])*2
            p[1] = 0
            continue
        ans+=p[i]*2
        p[i+1]-=p[i]
if p[N-1]>=0:
    ans+=p[N-1]
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
p=list(map(int,input().split()))
ans=0
for i in range(0,N-1,1):
    if p[i]>=0:
        if i==0 and p[0]>p[1]:
            ans += p[1]*2
            p[0] -= p[1]
            p[1] = 0
            ans += p[0]
            continue
        ans+=p[i]*2
        p[i+1]-=p[i]
if p[N-1]>=0:
    ans+=p[N-1]
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
*P, = map(int, input().split())
ans = 0
if P[0] > 0 and P[1] > 0:
    t = min(P[0], P[1])
    ans += t*2
    P[0] -= t
    P[1] -= t
for i in range(1, n-1):
    if P[i] > 0:
        ans += P[i]*2
        P[i+1] -= P[i]
        P[i] = 0
if P[0] > 0:
    ans += P[0]; P[0] = 0
if P[-1] > 0:
    ans += P[-1]; P[-1] = 0
print(ans, P)

Submission status: Wrong Answer

Solution code:
n = int(input())
*P, = map(int, input().split())
ans = 0
if P[0] > 0 and P[1] > 0:
    t = min(P[0], P[1])
    ans += t*2
    P[0] -= t
    P[1] -= t
for i in range(1, n-1):
    if P[i] > 0:
        ans += P[i]*2
        P[i+1] -= P[i]
        P[i] = 0
if P[0] > 0:
    ans += P[0]; P[0] = 0
if P[-1] > 0:
    ans += P[-1]; P[-1] = 0
print(ans)

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    length = int(input())
    hill = [int(x) for x in input().split()]
    count = 0
    while any([x > 0 for x in hill]):
        hill, c = step(hill)
        count += c
        # print(count, hill)
    print(count)

def step(hill):
    c = 0
    highest = max(hill)
    for (i, _) in ((i,x) for (i, x) in enumerate(hill) if x == highest):
        if 0 < i < len(hill)-1:
            if hill[i-1] > hill[i+1]:
                hill[i-1] -= 1
            else:
                hill[i+1] -= 1
            hill[i] -= 1
            c += 2

        elif i == 0:
            if hill[1] > 0:
                hill[1] -= 1
                c += 1
            hill[0] -= 1
            c += 1

        elif i == len(hill) - 1:
            if hill[-2] > 0:
                hill[-2] -= 1
                c += 1
            hill[-1] -= 1
            c += 1

        else:
            raise Exception("unreachable")
                            
    return (hill, c)
            

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    count = 0
    while any([x > 0 for x in hill]):
        hill, c = step(hill)
        count += c
        # print(count, hill)
    print(count)

def step(hill):
    c = 0
    highest = max(hill)
    for (i, _) in ((i, x) for (i, x) in enumerate(hill) if x == highest):
        if 0 < i < len(hill)-1:
            if hill[i] % 2 == hill[i-1] % 2:
                hill[i-1] -= 1
            elif hill[i] % 2 == hill[i+1] % 2:
                hill[i+1] -= 1
            else:
                if hill[i-1] > hill[i+1]:
                    hill[i-1] -= 1
                else:
                    hill[i+1] -= 1
            hill[i] -= 1
            c += 2

        elif i == 0:
            if hill[1] > 0:
                hill[1] -= 1
                c += 1
            hill[0] -= 1
            c += 1

        elif i == len(hill) - 1:
            if hill[-2] > 0:
                hill[-2] -= 1
                c += 1
            hill[-1] -= 1
            c += 1

        else:
            raise Exception("unreachable")

    return (hill, c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    count = 0
    while any([x > 0 for x in hill]):
        hill, c = step(hill)
        count += c
        # print(count, hill)
    print(count)

def step(hill):
    c = 0
    highest = max(hill)
    i = hill.index(highest)

    if 0 < i < len(hill)-1:
        if hill[i] % 2 == hill[i-1] % 2:
            hill[i-1] -= 1
        elif hill[i] % 2 == hill[i+1] % 2:
            hill[i+1] -= 1
        else:
            if hill[i-1] > hill[i+1]:
                hill[i-1] -= 1
            else:
                hill[i+1] -= 1
        hill[i] -= 1
        c += 2

    elif i == 0:
        if hill[1] > 0:
            hill[1] -= 1
            c += 1
        hill[0] -= 1
        c += 1

    elif i == len(hill) - 1:
        if hill[-2] > 0:
            hill[-2] -= 1
            c += 1
        hill[-1] -= 1
        c += 1

    else:
        raise Exception("unreachable")

    return (hill, c)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(len(hill)-1):
        if hill[i] > 0 and hill[i+1] >0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2
    
    return (hill, c)

def finalize(hill):
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(len(hill)-1):
        if hill[i] > 0 and hill[i+1] >0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2
    
    return (hill, c)

def finalize(hill):
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2

    if c == 0:
        i = 0
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2

    return (hill, c)

def finalize(hill):
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2

    i = 0
    if hill[i] > 0 and hill[i+1] > 0:
        scraped = min(hill[i], hill[i+1])
        hill[i] -= scraped
        hill[i+1] -= scraped
        c += 2

    return (hill, c)

def finalize(hill):
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2 * scraped

    i = 0
    if hill[i] > 0 and hill[i+1] > 0:
        scraped = min(hill[i], hill[i+1])
        hill[i] -= scraped
        hill[i+1] -= scraped
        c += 2

    return (hill, c)

def finalize(hill):
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2 * scraped

    i = 0
    if hill[i] > 0 and hill[i+1] > 0:
        scraped = min(hill[i], hill[i+1])
        hill[i] -= scraped
        hill[i+1] -= scraped
        c += 2 * scraped

    return (hill, c)

def finalize(hill):
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2 * scraped

    i = 0
    if hill[i] > 0 and hill[i+1] > 0:
        scraped = min(hill[i], hill[i+1])
        hill[i] -= scraped
        hill[i+1] -= scraped
        c += 2 * scraped

    return (hill, c)

def finalize(hill):
    assert(hill[0] == 0 or hill[1] == 0)
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
n = int(input())
*P, = map(int, input().split())
memo = {(n-1, i): i for i in range(P[-1]+1)}
def dfs(pos, t):
    if (pos, t) in memo:
        return memo[pos, t]
    res = memo[pos, t] = dfs(pos+1, max(0, P[pos+1]-t)) + t*2
    return res
print(min(dfs(0, P[0]-i)+i for i in range(P[0]+1)))

Submission status: Accepted

Solution code:
def solve():
    from sys import stdin
    f_i = stdin
    
    N = int(f_i.readline())
    P = list(map(int, f_i.readline().split()))
    left = P[0]
    
    ans = N * 6
    for i in range(left + 1):
        tP = P[:]
        tP[0] -= i
        t_ans = i
        for j in range(N - 1):
            tpj = tP[j]
            if tpj > 0:
                tP[j + 1] -= tpj
                t_ans += 2 * tpj
        if tP[-1] > 0:
            t_ans += tP[-1]
        if t_ans < ans:
            ans = t_ans
    
    print(ans)

solve()

Submission status: Accepted

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

ans = 10**9
for i in range(4):
    cnt = [0] * n
    cnt[0] += i
    for j in range(n-1):
        diff = pancake[j] - cnt[j]
        if diff > 0:
            cnt[j] += diff
            cnt[j+1] += diff
    diff = pancake[n-1] - cnt[n-1]
    if diff > 0:
        cnt[n-1] += diff

    if ans > sum(cnt):
        ans = sum(cnt)

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
*P, = map(int, input().split())
memo = {(n-1, i): i for i in range(P[-1]+1)}
def dfs(pos, t):
    if (pos, t) in memo:
        return memo[pos, t]
    res = memo[pos, t] = dfs(pos+1, max(0, P[pos+1]-t)) + t*2
    return res
print(min(dfs(0, P[0]-i)+i for i in range(P[0]+1)))

Submission status: Runtime Error

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2 * scraped

    i = 0
    if hill[i] > 0 and hill[i+1] > 0:
        scraped = min(hill[i], hill[i+1])
        hill[i] -= scraped
        hill[i+1] -= scraped
        c += 2 * scraped

    return (hill, c)

def finalize(hill):
    assert(hill[1] == 0)
    assert(hill[-2] == 0)
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
'''aizu 0340
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0340
'''
# encoding: 'utf-8'

def main():
    input()
    hill = [int(x) for x in input().split()]
    _hill = [0] * len(hill)
    count = 0
    while hill != _hill:
        # print(count, hill)
        _hill = hill
        hill, c = step(hill)
        count += c
    count += finalize(hill)
    print(count)

def step(hill):
    c = 0
    for i in range(1, len(hill)-1):
        if hill[i] > 0 and hill[i+1] > 0:
            scraped = min(hill[i], hill[i+1])
            hill[i] -= scraped
            hill[i+1] -= scraped
            c += 2 * scraped

    i = 0
    if hill[i] > 0 and hill[i+1] > 0:
        scraped = min(hill[i], hill[i+1])
        hill[i] -= scraped
        hill[i+1] -= scraped
        c += 2 * scraped

    return (hill, c)

def finalize(hill):
    assert(hill[1] == 0)
    c = hill[0] + hill[-1] + sum(hill[1:-1])*2
    return c

if __name__ == '__main__':
    main()