結果

問題 No.1391 ±1 Abs Sum
ユーザー Kiri8128Kiri8128
提出日時 2021-02-12 22:01:24
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 518 bytes
コンパイル時間 829 ms
コンパイル使用メモリ 86,944 KB
実行使用メモリ 259,660 KB
最終ジャッジ日時 2023-09-27 04:00:31
合計ジャッジ時間 16,659 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,272 KB
testcase_01 AC 76 ms
71,192 KB
testcase_02 AC 75 ms
71,484 KB
testcase_03 AC 74 ms
71,108 KB
testcase_04 AC 82 ms
71,348 KB
testcase_05 AC 75 ms
71,448 KB
testcase_06 AC 75 ms
71,468 KB
testcase_07 AC 75 ms
71,108 KB
testcase_08 AC 80 ms
75,492 KB
testcase_09 AC 79 ms
75,840 KB
testcase_10 AC 80 ms
75,748 KB
testcase_11 AC 486 ms
259,372 KB
testcase_12 AC 486 ms
259,076 KB
testcase_13 AC 470 ms
259,660 KB
testcase_14 AC 347 ms
258,700 KB
testcase_15 AC 360 ms
258,880 KB
testcase_16 AC 394 ms
258,816 KB
testcase_17 AC 366 ms
259,176 KB
testcase_18 AC 447 ms
259,504 KB
testcase_19 AC 409 ms
258,972 KB
testcase_20 WA -
testcase_21 AC 684 ms
259,376 KB
testcase_22 WA -
testcase_23 AC 778 ms
259,072 KB
testcase_24 AC 824 ms
258,520 KB
testcase_25 AC 737 ms
259,400 KB
testcase_26 AC 785 ms
259,496 KB
testcase_27 AC 616 ms
259,096 KB
testcase_28 AC 594 ms
247,948 KB
testcase_29 AC 700 ms
258,768 KB
testcase_30 AC 609 ms
259,252 KB
testcase_31 AC 474 ms
259,524 KB
testcase_32 AC 324 ms
240,108 KB
testcase_33 AC 346 ms
259,288 KB
testcase_34 AC 581 ms
259,220 KB
testcase_35 AC 74 ms
71,604 KB
testcase_36 AC 460 ms
258,872 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def f(k):
    b = A[k]
    L = sorted([abs(a - b) for a in A])
    return sum(L[:K]) - sum(L[K:])

N, K = map(int, input().split())
A = [int(a) for a in input().split()]
l, r = 0, N - 1
fl, fr = f(l), f(r)
ans = min(fl, fr)
while r - l > 3:
    m1 = (l * 3 + r) // 4
    m2 = (l + r * 3) // 4
    fm1, fm2 = f(m1), f(m2)
    if fm1 < fm2:
        r = m2
        ans = min(ans, fm1)
    else:
        l = m1
        ans = min(ans, fm2)
if r - l > 1:
    for i in range(l + 1, r):
        ans = min(ans, f(i))
print(ans)
0