結果

問題 No.2370 He ate many cakes
ユーザー wgrapewgrape
提出日時 2023-10-31 14:47:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 410 ms / 2,000 ms
コード長 1,950 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 81,868 KB
実行使用メモリ 83,588 KB
最終ジャッジ日時 2023-10-31 14:47:18
合計ジャッジ時間 3,926 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,732 KB
testcase_01 AC 41 ms
55,732 KB
testcase_02 AC 41 ms
55,732 KB
testcase_03 AC 252 ms
82,464 KB
testcase_04 AC 41 ms
55,732 KB
testcase_05 AC 41 ms
55,732 KB
testcase_06 AC 41 ms
55,732 KB
testcase_07 AC 41 ms
55,732 KB
testcase_08 AC 42 ms
55,732 KB
testcase_09 AC 41 ms
55,732 KB
testcase_10 AC 185 ms
83,580 KB
testcase_11 AC 253 ms
82,472 KB
testcase_12 AC 189 ms
83,588 KB
testcase_13 AC 266 ms
82,480 KB
testcase_14 AC 410 ms
82,484 KB
testcase_15 AC 261 ms
82,480 KB
testcase_16 AC 110 ms
77,904 KB
testcase_17 AC 261 ms
82,480 KB
testcase_18 AC 68 ms
76,192 KB
testcase_19 AC 57 ms
68,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

maxS = 0
minS = 0
for i in range(N):
    if A[i] > 0:
        maxS += A[i]
    else:
        minS += A[i]

A,B = A[:N // 2],A[N // 2:]

from collections import defaultdict
# {おいしさ:個数}を作る
def search(X):
    dic = defaultdict(int)
    n = len(X)
    for i in range(1 << n):
        val = 0
        for j in range(n):
            if (i >> j) & 1:
                val += X[j]
        dic[val] += 1
    return dic
                
A = search(A)
B = search(B)

def srt(X): # ある数以上の数が何個あるかを、ある数でソートした状態で返す
    X = sorted(X.items(), key = lambda x:x[0])
    n = len(X)
    X = [list(X[i]) for i in range(n)]
    for i in range(n - 1, 0, -1):
        X[i - 1][1] += X[i][1]
    num = [0] * n
    cnt = [0] * n
    for i in range(n):
        num[i], cnt[i] = X[i]
    return num, cnt

Bnum, Bcnt = srt(B)
import bisect
def is_ok(x): # x以上の数をK個以上作れればOK
    cnt = 0
#    print("x",x,"以上の値を",K,"個以上作れるか")
    for key, value in A.items():
#        print("Aのkey",key,"が",value,"個")
        target = x - key
        # target以上の数がいくつあるか
#        print("Bnum",Bnum,"にkey",key,"に対してtarget",target,"以上の数がいくつあるか")
        ind = bisect.bisect_left(Bnum, target)
#        print("Bcnt",Bcnt,"ind",ind,)
        if len(Bnum) == ind: # 作れない
#            print("作れない")
            continue
        cnt += Bcnt[ind] * value
        if cnt >= K:
#            print(cnt,"個作れた")
            return True
#    print("作れない")
    return False
        

ng = maxS + 1
ok = minS - 1
while ng - ok > 1:
    mid = (ng + ok) // 2
#    print("ok",ok,"ng",ng,"mid",mid)
#    if mid < 0 and (ok + ng) % 2 == 1:
#        mid -= 1
    if is_ok(mid):
        ok = mid
    else:
        ng = mid
        
print(ok)
0