結果

問題 No.198 キャンディー・ボックス2
ユーザー あかりきあかりき
提出日時 2021-03-15 00:07:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 185 ms / 1,000 ms
コード長 1,146 bytes
コンパイル時間 223 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 101,248 KB
最終ジャッジ日時 2024-04-24 05:16:07
合計ジャッジ時間 5,615 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 183 ms
87,584 KB
testcase_01 AC 176 ms
87,448 KB
testcase_02 AC 168 ms
96,072 KB
testcase_03 AC 157 ms
96,060 KB
testcase_04 AC 166 ms
95,812 KB
testcase_05 AC 179 ms
95,792 KB
testcase_06 AC 168 ms
95,612 KB
testcase_07 AC 157 ms
95,652 KB
testcase_08 AC 169 ms
95,784 KB
testcase_09 AC 71 ms
75,776 KB
testcase_10 AC 74 ms
75,912 KB
testcase_11 AC 89 ms
76,160 KB
testcase_12 AC 113 ms
101,248 KB
testcase_13 AC 167 ms
95,724 KB
testcase_14 AC 56 ms
64,128 KB
testcase_15 AC 60 ms
64,384 KB
testcase_16 AC 73 ms
75,764 KB
testcase_17 AC 78 ms
76,224 KB
testcase_18 AC 94 ms
75,628 KB
testcase_19 AC 127 ms
78,080 KB
testcase_20 AC 161 ms
87,620 KB
testcase_21 AC 168 ms
88,008 KB
testcase_22 AC 170 ms
87,776 KB
testcase_23 AC 155 ms
84,868 KB
testcase_24 AC 148 ms
90,436 KB
testcase_25 AC 172 ms
81,800 KB
testcase_26 AC 185 ms
87,368 KB
testcase_27 AC 182 ms
95,756 KB
testcase_28 AC 157 ms
82,432 KB
testcase_29 AC 179 ms
87,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

b=int(input())
n=int(input())
c=[int(input()) for i in range(n)]
c.sort()
ans=[]
if n//2==1:
  t=c[n//2]
  for i in range(max(t-10**5,0),t+10**5):
    ct=0
    y=b
    for j in range(n):
      if c[j]>=i:
        ct+=c[j]-i
        y+=c[j]-i
    for j in range(n):
      if c[j]<i:
        ct+=i-c[j]
        y-=i-c[j]
    if y>=0:
      ans.append(ct)
else:
  t1=c[n//2-1]
  t2=c[n//2]
  for i in range(max(t1-10**5,0),t1+10**5):
    ct=0
    y=b
    for j in range(n):
      if c[j]>=i:
        ct+=c[j]-i
        y+=c[j]-i
    for j in range(n):
      if c[j]<i:
        ct+=i-c[j]
        y-=i-c[j]
    if y>=0:
      ans.append(ct)
  for i in range(max(t2-10**5,0),t2+10**5):
    ct=0
    y=b
    for j in range(n):
      if c[j]>=i:
        ct+=c[j]-i
        y+=c[j]-i
    for j in range(n):
      if c[j]<i:
        ct+=i-c[j]
        y-=i-c[j]
    if y>=0:
      ans.append(ct)

x=sum(c)+b
t3=x//n
for i in range(max(t3-10**5,0),t3+10**5):
  ct=0
  y=b
  for j in range(n):
    if c[j]>=i:
      ct+=c[j]-i
      y+=c[j]-i
  for j in range(n):
    if c[j]<i:
      ct+=i-c[j]
      y-=i-c[j]
  if y>=0:
    ans.append(ct)
print(min(ans))
0