結果
| 問題 | 
                            No.1095 Smallest Kadomatsu Subsequence
                             | 
                    
| コンテスト | |
| ユーザー | 
                             titia
                         | 
                    
| 提出日時 | 2020-06-26 22:37:21 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 596 ms / 2,000 ms | 
| コード長 | 2,381 bytes | 
| コンパイル時間 | 334 ms | 
| コンパイル使用メモリ | 82,432 KB | 
| 実行使用メモリ | 202,940 KB | 
| 最終ジャッジ日時 | 2024-07-04 22:24:41 | 
| 合計ジャッジ時間 | 8,744 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 30 | 
ソースコード
N=int(input())
A=list(map(int,input().split()))
MINL=[]
X=10**9
for a in A:
    X=min(a,X)
    MINL.append(X)
X=10**9
MINR=[]
for a in A[::-1]:
    X=min(a,X)
    MINR.append(X)
MINR=MINR[::-1]
ANS=3*10**9
for i in range(1,N-1):
    if A[i]>MINL[i-1] and A[i]>MINR[i+1]:
        ANS=min(ANS,A[i]+MINL[i-1]+MINR[i+1])
SA=sorted(set(A))
compression_dict={a: ind for ind, a in enumerate(SA)}
A=[compression_dict[a] for a in A] # [2, 3, 0, 1, 4]
LEN=N+2 # 必要なら座標圧縮する
BIT=[0]*(LEN+1) # 1-indexedなtree
def update(v,w): # index vにwを加える
    while v<=LEN:
        BIT[v]+=w
        v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4
def getvalue(v): # [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS
def bisect_on_BIT(x): # [1,ind]の和がはじめてx以上になるindexを探す
    if x<=0:
        return 0
    
    ANS=0
    h=1<<(LEN.bit_length()-1) # LEN以下の最小の2ベキ
    while h>0:
        if ANS+h<=LEN and BIT[ANS+h]<x:
            x-=BIT[ANS+h]
            ANS+=h
        h//=2
    return ANS+1 # LENまでの和がx未満のとき, LEN+1を返すことに注意
for a in A:
    update(a+1,1)
BITL=[0]*(LEN+1) # 1-indexedなtree
def updateL(v,w): # index vにwを加える
    while v<=LEN:
        BITL[v]+=w
        v+=(v&(-v)) # 自分を含む大きなノードへ. たとえばv=3→v=4
def getvalueL(v): # [1,v]の区間の和を求める
    ANS=0
    while v!=0:
        ANS+=BITL[v]
        v-=(v&(-v)) # 自分より小さい2ベキのノードへ. たとえばv=3→v=2へ
    return ANS
def bisect_on_BITL(x): # [1,ind]の和がはじめてx以上になるindexを探す
    if x<=0:
        return 0
    
    ANS=0
    h=1<<(LEN.bit_length()-1) # LEN以下の最小の2ベキ
    while h>0:
        if ANS+h<=LEN and BITL[ANS+h]<x:
            x-=BITL[ANS+h]
            ANS+=h
        h//=2
    return ANS+1 # LENまでの和がx未満のとき, LEN+1を返すことに注意
for a in A:
    x=bisect_on_BIT(getvalue(a+1)+1)
    y=bisect_on_BITL(getvalueL(a+1)+1)
    if x!=LEN+1 and y!=LEN+1:
        ANS=min(ANS,SA[x-1]+SA[y-1]+SA[a])
    update(a+1,-1)
    updateL(a+1,1)
if ANS==3*10**9:
    print(-1)
else:
    print(ANS)
    
            
            
            
        
            
titia