結果

問題 No.484 収穫
ユーザー mkawa2
提出日時 2023-04-19 15:01:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,612 ms / 3,000 ms
コード長 1,508 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 82,588 KB
実行使用メモリ 245,272 KB
最終ジャッジ日時 2025-03-30 06:14:48
合計ジャッジ時間 19,420 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = (1 << 63)-1
# md = 10**9+7
md = 998244353

from heapq import *

def pack(l,r,s):
    p=l*n+r
    return p*2+s

def unpack(p):
    p,s=divmod(p,2)
    l,r=divmod(p,n)
    return l,r,s

n=II()
aa=LI()

hp=[]
dist=[inf]*(n*n*2)

p=pack(0,n-1,0)
dist[p]=aa[0]
heappush(hp,(aa[0],p))
p=pack(0,n-1,1)
dist[p]=aa[-1]
heappush(hp,(aa[-1],p))

def push(d,p):
    if d>=dist[p]:return
    dist[p]=d
    heappush(hp,(d,p))

while hp:
    d,p=heappop(hp)
    if d>dist[p]:continue
    l,r,s=unpack(p)
    if l==r:
        print(d)
        exit()
    if s:
        nd=max(d+1,aa[r-1])
        np=pack(l,r-1,1)
        push(nd,np)
        nd=max(d+r-l,aa[l])
        np=pack(l,r-1,0)
        push(nd,np)
    else:
        nd=max(d+1,aa[l+1])
        np=pack(l+1,r,0)
        push(nd,np)
        nd=max(d+r-l,aa[r])
        np=pack(l+1,r,1)
        push(nd,np)
0