結果

問題 No.198 キャンディー・ボックス2
ユーザー FromBooskaFromBooska
提出日時 2023-04-21 11:59:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 40 ms / 1,000 ms
コード長 902 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 53,932 KB
最終ジャッジ日時 2024-04-24 01:32:27
合計ジャッジ時間 2,005 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,996 KB
testcase_01 AC 35 ms
53,020 KB
testcase_02 AC 35 ms
52,308 KB
testcase_03 AC 33 ms
53,620 KB
testcase_04 AC 35 ms
52,856 KB
testcase_05 AC 32 ms
53,932 KB
testcase_06 AC 33 ms
53,352 KB
testcase_07 AC 34 ms
53,084 KB
testcase_08 AC 34 ms
53,472 KB
testcase_09 AC 32 ms
53,356 KB
testcase_10 AC 33 ms
52,536 KB
testcase_11 AC 34 ms
52,980 KB
testcase_12 AC 35 ms
53,280 KB
testcase_13 AC 38 ms
53,052 KB
testcase_14 AC 33 ms
52,876 KB
testcase_15 AC 34 ms
53,220 KB
testcase_16 AC 33 ms
52,532 KB
testcase_17 AC 34 ms
53,168 KB
testcase_18 AC 33 ms
53,068 KB
testcase_19 AC 34 ms
53,348 KB
testcase_20 AC 36 ms
52,440 KB
testcase_21 AC 36 ms
52,620 KB
testcase_22 AC 35 ms
52,752 KB
testcase_23 AC 33 ms
52,736 KB
testcase_24 AC 34 ms
53,376 KB
testcase_25 AC 35 ms
52,856 KB
testcase_26 AC 35 ms
53,352 KB
testcase_27 AC 35 ms
52,632 KB
testcase_28 AC 36 ms
53,484 KB
testcase_29 AC 35 ms
52,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# medianにすればいいか
# WA出た
# median総数に足りない場合はmedianを下げる必要があるな

B = int(input())
N = int(input())
C = []
for i in range(N):
    C.append(int(input()))
C.sort()
sumC = sum(C)

from math import floor, ceil
if N%2 == 1:
    median = C[N//2]
    median = min(median, floor((sumC+B)/N))
    ans = 0
    for i in range(N):
        ans += abs(C[i]-median)
    #print(median, ans)
else:
    median_low = floor((C[N//2-1]+C[N//2])/2)
    median_low = min(median_low, floor((sumC+B)/N)) 
    ans_low = 0
    for i in range(N):
        ans_low += abs(C[i]-median_low)
    #print(median_low, ans_low)
    median_high = ceil((C[N//2-1]+C[N//2])/2)
    median_high = min(median_high, floor((sumC+B)/N)) 
    ans_high = 0
    for i in range(N):
        ans_high += abs(C[i]-median_high)
    #print(median_high, ans_high)
    ans = min(ans_low, ans_high)
print(ans)
0