結果
| 問題 | 
                            No.198 キャンディー・ボックス2
                             | 
                    
| コンテスト | |
| ユーザー | 
                            👑  | 
                    
| 提出日時 | 2019-05-05 11:14:15 | 
| 言語 | Lua  (LuaJit 2.1.1734355927)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 1,000 ms | 
| コード長 | 388 bytes | 
| コンパイル時間 | 167 ms | 
| コンパイル使用メモリ | 6,944 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-06-24 12:04:54 | 
| 合計ジャッジ時間 | 1,093 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
local ior = io.input()
local b, n = ior:read("*n", "*n")
local t = {}
local sum = 0
for i = 1, n do
  local a = ior:read("*n")
  t[i], sum = a, sum + a
end
table.sort(t)
local midpos = math.floor((n + 1) / 2)
local dstval = t[midpos]
if(sum + b < dstval * n) then
  dstval = math.floor((sum + b) / n)
end
local cnt = 0
for i = 1, n do
  cnt = cnt + math.abs(t[i] - dstval)
end
print(cnt)