Problem:


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

 バームクーヘン(Baumkuchen)

JOI 君は妹のJOI 子ちゃんとJOI 美ちゃんと一緒におやつを食べようとしている．今日のおやつは3 人の大好物のバームクーヘンだ．

バームクーヘンは下図のような円筒形のお菓子である．3 人に分けるために，JOI 君は半径方向に刃を3回入れて，これを3 つのピースに切り分けなければならない．ただしこのバームクーヘンは本物の木材のように固いので，刃を入れるのは簡単ではない．そのためこのバームクーヘンにはあらかじめ $N$ 個の切れ込みが入っており，JOI 君は切れ込みのある位置でのみ切ることができる．切れ込みに1 から $N$ まで時計回りに番号をふったとき，$1 \leq i \leq N - 1$ に対し， $i$ 番目の切れ込みと$i + 1$ 番目の切れ込みの間の部分の大きさは $A_i$ である．また $N$ 番目の切れ込みと1 番目の切れ込みの間の部分の大きさは $A_N$ である．

図1: バームクーヘンの例 $N = 6, A_1 = 1, A_2 = 5, A_3 = 4, A_4 = 5, A_5 = 2, A_6 = 4$

課題

切れ込みの個数 $N$ と，各部分の大きさを表す整数 $A_1,...,A_N$ が与えられる．バームクーヘンを3 つに切り分けたときの，最も小さいピースの大きさの最大値を出力するプログラムを作成せよ．

入力

標準入力から以下のデータを読み込め．

 1 行目には，整数 $N$ が書かれている．これはバームクーヘンに $N$ 個の切れ込みがあることを表す．

 続く$N$ 行のうちの $i$ 行目$(1 \leq i \leq N)$ には，整数 $A_i$ が書かれている．これは $i$ 番目の切れ込みと $i + 1$ 番目の切れ込みの間の部分 ($i = N$ のときは $N$ 番目の切れ込みと1 番目の切れ込みの間の部分) の大きさが $A_i$ であることを表す．

出力

標準出力に，バームクーヘンを3 つに切り分けたときの，最も小さいピースの大きさの最大値を表す整数を1 行で出力せよ．

制限

すべての入力データは以下の条件を満たす．

$3 \leq N \leq 100000$

$1 \leq A_i \leq 1000000000 (1 \leq i \leq N)$

入出力例

  
入力例 1 
  

  

6
1
5
4
5
2
4

  
出力例 1 
  

  

6

図2: 1 番目の切れ込みと3 番目の切れ込みと5 番目の切れ込みで切るのが最善である．

  
入力例 2 
  

  

30
1
34
44
13
30
1
9
3
7
7
20
12
2
44
6
9
44
31
17
20
33
18
48
23
19
31
24
50
43
15

  
出力例 2 
  

  

213

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
6
1
5
4
5
2
4


Sample output:
6

Submission status: Runtime Error

Solution code:
def d(p, q):
    return pos[p] - pos[q]

def solve():
    p0, p1, p2 = 0, 0, 0
    best = 0
    fail = False
    while p0 < N:
        if fail:
            p0 += 1
            # p2????±???????
            while d(p2, p1) > d(p1, p0) and p2 > p1:
                p2 -= 1
            fail = False
        # p1????±???????
        if p1 <= p0: p1 = p0 + 1
        while d(p1, p0) <= best and p1 - p0 < N:
            p1 += 1
        if d(p1, p0) <= best:
            fail = True
            continue
        # p2????±???????
        if p2 <= p1: p2 = p1 + 1
        while d(p2,p1) < d(p1, p0) and p2 - p0 < N:
            p2 += 1
        if d(p2, p1) < d(p1, p0):
            fail = True
            continue
        # check
        if L - d(p2, p0) >= d(p1, p0):
            best = d(p1, p0)
            if best >= L//3:
                return best
        else:
            fail = True
    return best

N = int(input())
A = [0] * N
L = 0
for i in range(N):
    A[i] = int(input())
    L += A[i]
pos = [0] * (2*N)
for i in range(1, 2*N):
    pos[i] = pos[i-1] + A[(i-1)%N]

print(solve())

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    sm = sum(a)
    tgt = sm/3

    i = 0
    ans = 0
    llow = 0
    lmid = 0
    lhigh = 0
    ltemp = 0
    rlow = 0
    rmid = 0
    rhigh = 0
    rtemp = 1
    tempsum = 0

    while rmid <= tgt:
        rmid += a[rtemp]
        rtemp += 1
    rlow = rmid - a[rtemp-1]
    rhigh = rmid + a[rtemp]

    while lmid <= tgt:
        lmid += a[ltemp]
        ltemp -= 1
    llow = lmid - a[ltemp+1]
    lhigh = lmid + a[ltemp]
    ltemp += 1

    ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
     min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
     min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
     min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

    tempsum += a[i]
    i += 1

    while tempsum <= tgt:
        lmid += a[i]
        rmid -= a[i]

        while rmid <= tgt:
            rmid += a[rtemp]
            rtemp += 1
        rlow = rmid - a[rtemp-1]
        rhigh = rmid + a[rtemp]

        while lmid >= tgt:
            lmid -= a[ltemp]
            ltemp += 1
        llow = lmid - a[ltemp]
        lhigh = lmid + a[ltemp-1]

        ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
         min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
         min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
         min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

        tempsum += a[i]
        i += 1

    print(ans)

main()

Submission status: Runtime Error

Solution code:

n = int(input())
a = [int(input()) for _ in range(n)]
sm = sum(a)
tgt = sm/3

i = 0
ans = 0
llow = 0
lmid = 0
lhigh = 0
ltemp = 0
rlow = 0
rmid = 0
rhigh = 0
rtemp = 1
tempsum = 0

while rmid <= tgt:
    rmid += a[rtemp]
    rtemp += 1
rlow = rmid - a[rtemp-1]
rhigh = rmid + a[rtemp]

while lmid <= tgt:
    lmid += a[ltemp]
    ltemp -= 1
llow = lmid - a[ltemp+1]
lhigh = lmid + a[ltemp]
ltemp += 1

ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
 min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
 min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
 min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

tempsum += a[i]
i += 1

while tempsum <= tgt:
    lmid += a[i]
    rmid -= a[i]

    while rmid <= tgt:
        rmid += a[rtemp]
        rtemp += 1
    rlow = rmid - a[rtemp-1]
    rhigh = rmid + a[rtemp]

    while lmid >= tgt:
        lmid -= a[ltemp]
        ltemp += 1
    llow = lmid - a[ltemp]
    lhigh = lmid + a[ltemp-1]

    ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
     min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
     min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
     min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

    tempsum += a[i]
    i += 1

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
sm = sum(a)
tgt = sm/3

i = 0
ans = 0
llow = 0
lmid = 0
lhigh = 0
ltemp = 0
rlow = 0
rmid = 0
rhigh = 0
rtemp = 1
tempsum = 0

while rmid <= tgt:
    rmid += a[rtemp]
    rtemp += 1
rlow = rmid - a[rtemp-1]
rhigh = rmid + a[rtemp]

while lmid <= tgt:
    lmid += a[ltemp]
    ltemp -= 1
llow = lmid - a[ltemp+1]
lhigh = lmid + a[ltemp]
ltemp += 1

ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
 min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
 min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
 min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

tempsum += a[i]
i += 1

while tempsum <= tgt:
    lmid += a[i]
    rmid -= a[i]

    while rmid <= tgt:
        rmid += a[rtemp]
        rtemp += 1
    rlow = rmid - a[rtemp-1]
    rhigh = rmid + a[rtemp]

    while lmid >= tgt:
        lmid -= a[ltemp]
        ltemp += 1
    llow = lmid - a[ltemp]
    lhigh = lmid + a[ltemp-1]

    ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
     min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
     min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
     min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

    tempsum += a[i]
    i += 1

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
sm = sum(a)
tgt = sm//3

i = 0
ans = 0
llow = 0
lmid = 0
lhigh = 0
ltemp = 0
rlow = 0
rmid = 0
rhigh = 0
rtemp = 1
tempsum = 0

while rmid <= tgt:
    rmid += a[rtemp]
    rtemp += 1
rlow = rmid - a[rtemp-1]
rhigh = rmid + a[rtemp]

while lmid <= tgt:
    lmid += a[ltemp]
    ltemp -= 1
llow = lmid - a[ltemp+1]
lhigh = lmid + a[ltemp]
ltemp += 1

ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
 min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
 min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
 min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

tempsum += a[i]
i += 1

while tempsum <= tgt:
    lmid += a[i]
    rmid -= a[i]

    while rmid <= tgt:
        rmid += a[rtemp]
        rtemp += 1
    rlow = rmid - a[rtemp-1]
    rhigh = rmid + a[rtemp]

    while lmid >= tgt:
        lmid -= a[ltemp]
        ltemp += 1
    llow = lmid - a[ltemp]
    lhigh = lmid + a[ltemp-1]

    ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),
     min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),
     min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),
     min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

    tempsum += a[i]
    i += 1

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
sm = sum(a)
tgt = sm//3

i = 0
ans = 0
llow = 0
lmid = 0
lhigh = 0
ltemp = 0
rlow = 0
rmid = 0
rhigh = 0
rtemp = 1
tempsum = 0

while rmid <= tgt:
    rmid += a[rtemp]
    rtemp += 1
rlow = rmid - a[rtemp-1]
rhigh = rmid + a[rtemp]

while lmid <= tgt:
    lmid += a[ltemp]
    ltemp -= 1
llow = lmid - a[ltemp+1]
lhigh = lmid + a[ltemp]
ltemp += 1

ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh), min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid), min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

tempsum += a[i]
i += 1

while tempsum <= tgt:
    lmid += a[i]
    rmid -= a[i]

    while rmid <= tgt:
        rmid += a[rtemp]
        rtemp += 1
    rlow = rmid - a[rtemp-1]
    rhigh = rmid + a[rtemp]

    while lmid >= tgt:
        lmid -= a[ltemp]
        ltemp += 1
    llow = lmid - a[ltemp]
    lhigh = lmid + a[ltemp-1]

    ans = max(ans,min(llow,rlow,sm-llow-rlow),min(llow,rhigh,sm-llow-rhigh),min(lhigh,rlow,sm-lhigh-rlow),min(lhigh,rhigh,sm-lhigh-rhigh),min(llow,rmid,sm-llow-rmid),min(lmid,rmid,sm-lmid-rmid),min(lhigh,rmid,sm-lhigh-rmid),min(lmid,rlow,sm-lmid-rlow),min(lmid,rhigh,sm-lmid-rhigh))

    tempsum += a[i]
    i += 1

print(ans)