結果

問題 No.2370 He ate many cakes
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-01-22 01:29:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 1,663 bytes
コンパイル時間 249 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 77,220 KB
最終ジャッジ日時 2024-01-22 01:29:47
合計ジャッジ時間 2,856 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
53,460 KB
testcase_01 AC 35 ms
53,460 KB
testcase_02 AC 35 ms
53,460 KB
testcase_03 AC 123 ms
77,220 KB
testcase_04 AC 34 ms
53,460 KB
testcase_05 AC 40 ms
53,460 KB
testcase_06 AC 36 ms
53,460 KB
testcase_07 AC 34 ms
53,460 KB
testcase_08 AC 34 ms
53,460 KB
testcase_09 AC 34 ms
53,460 KB
testcase_10 AC 85 ms
76,888 KB
testcase_11 AC 93 ms
77,220 KB
testcase_12 AC 114 ms
76,888 KB
testcase_13 AC 95 ms
77,220 KB
testcase_14 AC 113 ms
77,220 KB
testcase_15 AC 106 ms
77,220 KB
testcase_16 AC 98 ms
77,016 KB
testcase_17 AC 104 ms
77,220 KB
testcase_18 AC 53 ms
66,280 KB
testcase_19 AC 52 ms
66,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/2370


def solve(n1_array, n2_array, border):
    ans = 0
    for n2 in n2_array:
        if border - n2 > n1_array[-1]:
            continue
 
        low = 0
        high = len(n1_array) - 1
        while high - low > 1:
            mid = (high + low) // 2
            if n1_array[mid] >= border - n2:
                high = mid
            else: 
                low = mid
        if n1_array[low] >= border - n2:
            ans += len(n1_array) - low
        else:
            ans += len(n1_array) - high

    return ans


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    if N == 1:
        array = [0, A[0]]
        array.sort(reverse=True)
        print(array[K - 1])
        return
    
    # 半分全列挙
    N1 = N // 2
    N2 = N - N1

    n1_array = []
    for bit in range(2 ** N1):
        ans = 0
        for i in range(N1):
            if bit & (1 << i) > 0:
                ans += A[i]
        n1_array.append(ans)
    n1_array.sort()

    n2_array = []
    for bit in range(2 ** N2):
        ans = 0
        for i in range(N2):
            if bit & (1 << i) > 0:
                ans += A[i + N1]
        n2_array.append(ans)
    
    # 二部探索によって値を出す
    low = sum([a for a in A if a < 0])
    high = sum([a for a in A if a > 0])
    while high - low > 1:
        mid = (high + low) // 2
        if solve(n1_array, n2_array, mid) >= K:
            low = mid
        else:
            high = mid
    if solve(n1_array, n2_array, high) >= K:
        print(high)
    else:
        print(low)

        



if __name__ == "__main__":
    main()
0