結果

問題 No.198 キャンディー・ボックス2
コンテスト
ユーザー るこーそー
提出日時 2024-09-20 13:12:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 36 ms / 1,000 ms
コード長 349 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 82,484 KB
実行使用メモリ 54,256 KB
最終ジャッジ日時 2025-11-26 11:47:45
合計ジャッジ時間 2,255 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

b=int(input())
n=int(input())
C=[int(input()) for _ in range(n)]

def cnt(x):
    return sum(abs(c-x) for c in C)

ng,ok=-1,(sum(C)+b)//n
while ok-ng>3:
    mid1=(ng*2+ok)//3
    mid2=(ng+ok*2)//3
    
    if cnt(mid1)<cnt(mid2):
        ok=mid2
    else:
        ng=mid1

ans=float('INF')
for i in range(ng,ok+1):
    ans=min(ans,cnt(i))
print(ans)
0