結果

問題 No.1391 ±1 Abs Sum
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-05-11 13:50:35
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 517 bytes
コンパイル時間 635 ms
コンパイル使用メモリ 81,296 KB
実行使用メモリ 258,996 KB
最終ジャッジ日時 2023-10-21 17:54:41
合計ジャッジ時間 23,210 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,520 KB
testcase_01 AC 39 ms
53,520 KB
testcase_02 AC 36 ms
53,520 KB
testcase_03 AC 40 ms
59,652 KB
testcase_04 AC 38 ms
53,520 KB
testcase_05 WA -
testcase_06 AC 37 ms
53,520 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 493 ms
137,040 KB
testcase_15 AC 290 ms
134,320 KB
testcase_16 AC 649 ms
159,236 KB
testcase_17 AC 316 ms
133,908 KB
testcase_18 AC 743 ms
162,996 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 413 ms
184,944 KB
testcase_32 AC 259 ms
198,600 KB
testcase_33 AC 320 ms
257,604 KB
testcase_34 WA -
testcase_35 AC 36 ms
53,520 KB
testcase_36 AC 633 ms
258,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k = map(int,input().split())
A = list(map(int,input().split()))

def calc(x):
    count = []
    for a in A:
        count.append(abs(x-a))
    count.sort()
    ans = 0
    for i in range(n):
        if i < k:
            ans += count[i]
        else:
            ans -= count[i]
    return ans


l = A[0]-1
r = A[-1]+1
while r > l+1:
    m1 = (r+2*l)//3
    m2 = (2*r+l)//3
    c1 = calc(m1)
    c2 = calc(m2)
    if c1 <= c2:
        r = m2
    else:
        l = m2
ans = 10**20
print(min(calc(A[0]),calc(A[-1])))
0