結果

問題 No.1391 ±1 Abs Sum
ユーザー so4649so4649
提出日時 2021-02-12 22:11:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 707 bytes
コンパイル時間 1,656 ms
コンパイル使用メモリ 86,832 KB
実行使用メモリ 111,120 KB
最終ジャッジ日時 2023-09-27 04:25:36
合計ジャッジ時間 6,580 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,200 KB
testcase_01 AC 72 ms
71,260 KB
testcase_02 AC 71 ms
71,276 KB
testcase_03 AC 72 ms
71,300 KB
testcase_04 AC 73 ms
71,104 KB
testcase_05 AC 73 ms
71,276 KB
testcase_06 AC 72 ms
71,412 KB
testcase_07 AC 73 ms
71,128 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 163 ms
111,120 KB
testcase_12 AC 136 ms
106,068 KB
testcase_13 AC 154 ms
108,272 KB
testcase_14 AC 116 ms
96,356 KB
testcase_15 AC 120 ms
98,764 KB
testcase_16 AC 130 ms
105,876 KB
testcase_17 AC 123 ms
101,984 KB
testcase_18 AC 145 ms
107,580 KB
testcase_19 AC 135 ms
102,544 KB
testcase_20 AC 160 ms
110,496 KB
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 152 ms
110,876 KB
testcase_32 AC 112 ms
96,544 KB
testcase_33 AC 116 ms
98,524 KB
testcase_34 AC 158 ms
108,748 KB
testcase_35 AC 80 ms
71,320 KB
testcase_36 AC 154 ms
111,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

k = n-k

ans = 10**15

b = [1]*(n-k)+[-1]*k
ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i]+b[i]
base = 0
while base+1 < n and ruiseki[base+1] < (n-2*k+1)//2:
    base += 1
left = sum(abs(a[i]-a[base]) for i in range(n-k))+sum(-abs(a[base]-a[i]) for i in range(n-k,n))
# print(base,left)

b = [-1]*k+[1]*(n-k)
ruiseki = [0]*(n+1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i]+b[i]
base = n-1
while 0 < base and ruiseki[n]-ruiseki[base] < (n-2*k+1)//2:
    base -= 1
right = sum(-abs(a[i]-a[base]) for i in range(k))+sum(abs(a[base]-a[i]) for i in range(k,n))
# print(base,right)

ans = min(ans,left,right)
print(ans)
0