結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー Mine
提出日時 2020-08-30 16:39:30
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,072 bytes
コンパイル時間 140 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 32,628 KB
最終ジャッジ日時 2024-11-15 11:23:52
合計ジャッジ時間 8,453 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19 WA * 11
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
a = list(map(int, input().split()))
lminList = [a[0]]
rminList = [a[-1]]
lmaxList = [a[0]]
rmaxList = [a[-1]]
lmin = a[0]
rmin = a[-1]
lmax = a[0]
rmax = a[-1]

for i in range(1, n):
    if  lmin > a[i]:
        lminList.append(a[i])
        lmin = a[i]
    else:
        lminList.append(lmin)
    if lmax < a[i]:
        lmaxList.append(a[i])
        lmax = a[i]
    else:
        lmaxList.append(lmax)

for i in range(n-1)[::-1]:
    if  rmin > a[i]:
        rminList.append(a[i])
        rmin = a[i]
    else:
        rminList.append(rmin)
    if rmax < a[i]:
        rmaxList.append(a[i])
        rmax = a[i]
    else:
        rmaxList.append(rmax)

rminList = rminList[::-1]
rmaxList = rmaxList[::-1]

ans = float("inf")
for i in range(1, n-1):
    lmin = lminList[i-1]
    rmin = rminList[i+1]
    lmax = lmaxList[i-1]
    rmax = rmaxList[i+1]
    m = a[i]

    if lmin < m and m > rmin:
        ans = min(ans, lmin+m+rmin)
    if lmax > m and m < rmax:
        ans = min(ans, lmax+m+rmax)

if ans == float("inf"):
    print(-1)
else:
    print(ans)
0