結果

問題 No.2370 He ate many cakes
ユーザー wgrape
提出日時 2023-10-31 15:51:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 394 ms / 2,000 ms
コード長 1,946 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 84,136 KB
最終ジャッジ日時 2024-09-25 17:41:06
合計ジャッジ時間 3,826 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

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
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