結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー rlangevinrlangevin
提出日時 2023-01-22 17:11:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,078 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 307 ms
コンパイル使用メモリ 86,952 KB
実行使用メモリ 108,452 KB
最終ジャッジ日時 2023-09-07 02:48:05
合計ジャッジ時間 15,620 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,196 KB
testcase_01 AC 70 ms
71,092 KB
testcase_02 AC 69 ms
71,416 KB
testcase_03 AC 76 ms
75,632 KB
testcase_04 AC 75 ms
75,712 KB
testcase_05 AC 76 ms
75,668 KB
testcase_06 AC 76 ms
75,564 KB
testcase_07 AC 76 ms
75,588 KB
testcase_08 AC 75 ms
75,700 KB
testcase_09 AC 75 ms
75,768 KB
testcase_10 AC 80 ms
75,796 KB
testcase_11 AC 76 ms
75,708 KB
testcase_12 AC 77 ms
75,628 KB
testcase_13 AC 180 ms
79,148 KB
testcase_14 AC 172 ms
78,956 KB
testcase_15 AC 171 ms
78,676 KB
testcase_16 AC 174 ms
79,056 KB
testcase_17 AC 173 ms
79,084 KB
testcase_18 AC 168 ms
78,636 KB
testcase_19 AC 174 ms
78,548 KB
testcase_20 AC 170 ms
78,628 KB
testcase_21 AC 172 ms
79,068 KB
testcase_22 AC 180 ms
78,708 KB
testcase_23 AC 1,070 ms
108,288 KB
testcase_24 AC 1,060 ms
108,264 KB
testcase_25 AC 1,075 ms
108,360 KB
testcase_26 AC 1,062 ms
108,304 KB
testcase_27 AC 1,078 ms
108,452 KB
testcase_28 AC 440 ms
108,228 KB
testcase_29 AC 508 ms
108,228 KB
testcase_30 AC 766 ms
108,212 KB
testcase_31 AC 750 ms
108,280 KB
testcase_32 AC 739 ms
108,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self, size, f=min, default=10 ** 18):
        self.size = 2**(size-1).bit_length() 
        self.default = default
        self.dat = [default]*(self.size*2) 
        self.f = f

    def update(self, i, x):
        i += self.size
        self.dat[i] = x
        while i > 0:
            i >>= 1
            self.dat[i] = self.f(self.dat[i*2], self.dat[i*2+1])

    def query(self, l, r):
        l += self.size
        r += self.size
        lres, rres = self.default, self.default
        while l < r:
            if l & 1:
                lres = self.f(lres, self.dat[l])
                l += 1

            if r & 1:
                r -= 1
                rres = self.f(self.dat[r], rres) 
            l >>= 1
            r >>= 1
        res = self.f(lres, rres)
        return res


N = int(input())
A = list(map(int, input().split()))
B = [0] * N
for i in range(N):
    B[i] = (A[i], i + 1)
B.sort(reverse=True)

inf = 10 ** 18
T = SegmentTree(N + 2)
ans = inf
for a, i in B:
    x = T.query(0, i)
    y = T.query(i + 1, N + 2)
    ans = min(ans, x + a + y)
    T.update(i, a)
    
for i, a in enumerate(A):
    i += 1
    x = T.query(0, i)
    y = T.query(i + 1, N + 2)
    if x > a or y > a:
        continue
    ans = min(ans, x + a + y)

print(ans) if ans < inf else print(-1)
0