結果

問題 No.1391 ±1 Abs Sum
ユーザー chineristACchineristAC
提出日時 2020-11-06 22:58:57
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 769 bytes
コンパイル時間 414 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 105,676 KB
最終ジャッジ日時 2023-09-29 19:31:19
合計ジャッジ時間 5,343 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,336 KB
testcase_01 AC 67 ms
71,160 KB
testcase_02 AC 62 ms
71,476 KB
testcase_03 AC 62 ms
71,352 KB
testcase_04 AC 65 ms
71,344 KB
testcase_05 AC 63 ms
71,192 KB
testcase_06 AC 62 ms
71,304 KB
testcase_07 AC 66 ms
71,308 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 118 ms
100,992 KB
testcase_12 AC 99 ms
102,728 KB
testcase_13 AC 113 ms
99,052 KB
testcase_14 AC 93 ms
94,452 KB
testcase_15 AC 93 ms
96,920 KB
testcase_16 AC 98 ms
102,756 KB
testcase_17 AC 96 ms
99,528 KB
testcase_18 AC 114 ms
100,384 KB
testcase_19 AC 99 ms
99,420 KB
testcase_20 AC 114 ms
101,064 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 116 ms
101,376 KB
testcase_32 AC 98 ms
94,192 KB
testcase_33 AC 106 ms
97,248 KB
testcase_34 AC 114 ms
101,128 KB
testcase_35 AC 66 ms
71,104 KB
testcase_36 AC 116 ms
103,192 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

s = (0,N-K)
for i in s:
    first = (N-K-i) - i - K
    last = (N-K-i) - i + K - 2

    if first>=0:
        mid = i
    elif last<=0:
        mid = i + K -1
    else:
        mid = K + 2*i - ((N+1)//2)

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

    res = min(res,tmp)

print(res)
0