結果

問題 No.198 キャンディー・ボックス2
ユーザー FromBooska
提出日時 2023-03-02 18:54:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 43 ms / 1,000 ms
コード長 716 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 59,616 KB
最終ジャッジ日時 2024-09-17 12:54:17
合計ジャッジ時間 2,429 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

# 谷、つまり1極値を求める三分探索での解き方
# https://roiti46.hatenablog.com/entry/2015/04/29/yukicoder_No.198_%E3%82%AD%E3%83%A3%E3%83%B3%E3%83%87%E3%82%A3%E3%83%BC%E3%83%BB%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9%EF%BC%92
# https://qiita.com/ganyariya/items/1553ff2bf8d6d7789127

B = int(input())
N = int(input())
C = [int(input()) for i in range(N)]

ans = 10**18
l, r = -1, 10**18
while r-l > 2:
    lm, rm = (2*l+r)//3, (l+2*r)//3
    if sum([lm-c for c in C]) > B:
        r = lm
    elif sum([rm-c for c in C]) > B:
        r = rm
    elif sum([abs(lm-c) for c in C]) <= sum([abs(rm-c) for c in C]):
        r = rm
    else:
        l = lm

ans = sum([abs((r+l)//2 - c) for c in C])
print(ans)
0