結果

問題 No.1391 ±1 Abs Sum
ユーザー convexineq
提出日時 2021-02-13 14:26:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 185 ms / 2,000 ms
コード長 453 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 122,012 KB
最終ジャッジ日時 2024-07-20 17:12:52
合計ジャッジ時間 6,004 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

def f(L,R,x):
    return -g(0,n,x) + 2*g(L,R,x)

def g(i,j,x):
    return h(i,x)*(1 if i <= x else -1) + h(j,x)*(-1 if j <= x else 1)

def h(j,x):
    return a[x]*(x-j) - (s[j]-s[x])

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

s = a + [0]
for i in range(n)[::-1]:
    s[i] += s[i+1]

L = 0
ans = 1<<60
for i in range(n):
    while L+k < n and a[i]-a[L] > a[L+k]-a[i]:
        L += 1
    v = f(L,L+k,i)
    ans = min(v,ans)
print(ans)
0