結果

問題 No.1391 ±1 Abs Sum
ユーザー chineristACchineristAC
提出日時 2020-11-08 19:28:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 666 bytes
コンパイル時間 342 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 109,364 KB
最終ジャッジ日時 2023-09-29 21:37:19
合計ジャッジ時間 7,499 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,200 KB
testcase_01 AC 70 ms
71,368 KB
testcase_02 AC 69 ms
71,080 KB
testcase_03 AC 71 ms
71,204 KB
testcase_04 AC 72 ms
71,196 KB
testcase_05 AC 71 ms
71,056 KB
testcase_06 WA -
testcase_07 AC 69 ms
71,304 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 154 ms
109,364 KB
testcase_12 AC 123 ms
102,972 KB
testcase_13 WA -
testcase_14 AC 125 ms
98,264 KB
testcase_15 WA -
testcase_16 AC 146 ms
102,748 KB
testcase_17 WA -
testcase_18 AC 143 ms
100,032 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 145 ms
104,784 KB
testcase_32 AC 104 ms
94,328 KB
testcase_33 AC 108 ms
97,156 KB
testcase_34 WA -
testcase_35 AC 70 ms
71,064 KB
testcase_36 AC 124 ms
103,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
A = list(map(int,input().split()))

if not K:
    tmp1 = -sum(abs(A[i]-A[0]) for i in range(N))
    tmp2 = -sum(abs(A[i]-A[N-1]) for i in range(N))
    exit(print(min(tmp1,tmp2)))

imos = [A[i] for i in range(N)]
for i in range(1,N):
    imos[i] += imos[i-1]

res = 10**18
l = 0
for i in range(N-K+1):
    while l+K < N and A[l+K] - A[i] < A[i] - A[l]:
        l += 1

    tmp = 0
    tmp += imos[l-1] * (l > 0) - A[i] * l
    tmp += A[i] * (i - l + 1) - imos[i] + imos[l-1] * (l > 0)
    tmp += imos[l+K-1] - imos[i] - A[i] * (l + K - i - 1)
    tmp += A[i] * (N - l - K) - imos[N-1] + imos[l+K-1]

    res = min(res,tmp)

print(res)
0