結果

問題 No.198 キャンディー・ボックス2
コンテスト
ユーザー rocoder
提出日時 2017-08-08 10:21:54
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
WA  
実行時間 -
コード長 633 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 329 ms
コンパイル使用メモリ 20,824 KB
実行使用メモリ 21,760 KB
最終ジャッジ日時 2026-04-28 10:23:25
合計ジャッジ時間 4,269 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other AC * 2 WA * 7 TLE * 1 -- * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# your code goes here
def ab(num):
    if num<0:
        num*=-1
    return num
B=int(input())
N=int(input())
C=[int(input()) for i in range(N)]
C.sort(reverse=True)
#print(C)
S=0
for i in range(N):
    S+=C[i]
#    print(S)
S1=S+B
#print(S1)
S1//=N
S//=N
mi=0
for i in range(N):
    mi+=ab(S-C[i])
i=S-3
if i<C[0]:
    i=C[0]
#print(S1)
while i<=S1 and i>=C[0]:
    M=0
    j=0
    #print(B)
    while B>0 and j<N:
        ma=ab(i-C[j])
     #   print(ma)
        B-=ma
        M+=ma
        j+=1
    if B<0:
        j-=1
        M+=B
        C[j]+=B
    while j<N:
        M+=ab(C[j]-i)
    if M<mi:
        mi=M
    i+=1
print(mi)
0