結果

問題 No.1391 ±1 Abs Sum
ユーザー terry_u16
提出日時 2021-02-12 22:32:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 762 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 107,080 KB
最終ジャッジ日時 2024-07-19 23:12:34
合計ジャッジ時間 5,127 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8 WA * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

current = 0

for i in range(n):
    if i < k:
        current += abs(a[i] - a[0])
    else:
        current -= abs(a[i] - a[0])

minimum = current
nextSub = k

for i in range(1, n):
    plus = n - max(nextSub - 1, i) + i - min(i, nextSub - k)
    minus = n - plus
    current += (plus - minus) * abs(a[i] - a[i - 1])

    while nextSub < n:
        left = a[nextSub - k]
        right = a[nextSub]
        leftDiff = abs(left - a[i])
        rightDiff = abs(right - a[i])

        if leftDiff > rightDiff:
            current -= 2 * leftDiff
            current += 2 * rightDiff
            nextSub += 1
        else:
            break
    
    minimum = min(minimum, current)

print(minimum)
0