結果

問題 No.1391 ±1 Abs Sum
ユーザー shotoyooshotoyoo
提出日時 2021-02-12 22:09:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 591 bytes
コンパイル時間 172 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 126,864 KB
最終ジャッジ日時 2024-07-19 22:05:29
合計ジャッジ時間 5,575 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")

n,k = list(map(int, input().split()))
a = list(map(int, input().split()))
cum = [0]
v = 0
for i in range(n):
    v += a[i]
    cum.append(v)
l = 0
ans = float("inf")
for i in range(n):
    x = a[i]
    while l<i and l<n-k and x - a[l] > a[k+l] - x:
        l += 1
    val = cum[l] - (cum[i] - cum[l]) + (cum[k+l] - cum[i]) - (cum[n] - cum[k+l])
    ans = min(ans, val + x * (n - 2*k - 4*l + 2*i))
#     print(i,l,val,x * (n - 2*k - 4*l + 2*i))
print(ans)
0