結果

問題 No.1391 ±1 Abs Sum
ユーザー persimmon-persimmonpersimmon-persimmon
提出日時 2021-02-13 10:14:52
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 677 bytes
コンパイル時間 270 ms
コンパイル使用メモリ 86,844 KB
実行使用メモリ 109,092 KB
最終ジャッジ日時 2023-09-27 18:23:28
合計ジャッジ時間 7,684 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,436 KB
testcase_01 AC 68 ms
71,396 KB
testcase_02 AC 66 ms
71,232 KB
testcase_03 AC 65 ms
71,300 KB
testcase_04 AC 67 ms
71,252 KB
testcase_05 AC 66 ms
71,312 KB
testcase_06 AC 66 ms
71,548 KB
testcase_07 AC 68 ms
71,440 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 141 ms
109,092 KB
testcase_12 AC 124 ms
104,636 KB
testcase_13 AC 135 ms
106,444 KB
testcase_14 AC 103 ms
94,964 KB
testcase_15 AC 106 ms
96,288 KB
testcase_16 AC 116 ms
102,568 KB
testcase_17 AC 109 ms
98,696 KB
testcase_18 AC 124 ms
100,848 KB
testcase_19 AC 120 ms
102,152 KB
testcase_20 AC 141 ms
107,772 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 144 ms
107,756 KB
testcase_32 AC 92 ms
92,220 KB
testcase_33 AC 97 ms
96,796 KB
testcase_34 RE -
testcase_35 AC 67 ms
71,076 KB
testcase_36 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

n,k=map(int,input().split())
a=list(map(int,input().split()))
inf=float('inf')
ans=inf
# 右端に-1を固める
tmp0=0
tmp1=0
for x in a:
  if a[k]-x>0:
    tmp1+=a[k]-x
  else:
    tmp0+=x-a[k]
ans=min(ans,tmp1-tmp0)
for i in reversed(range(k)):
  tmp0+=(n-k)*(a[i+1]-a[i])
  tmp1-=(i+1)*(a[i+1]-a[i])
  tmp1+=(k-i-1)*(a[i+1]-a[i])
  ans=min(ans,tmp1-tmp0)

# 左端に-1を固める。
a=[-x for x in a]
a.reverse()
tmp0=0
tmp1=0
for x in a:
  if a[k]-x>0:
    tmp1+=a[k]-x
  else:
    tmp0+=x-a[k]
ans=min(ans,tmp1-tmp0)
for i in reversed(range(k)):
  tmp0+=(n-k)*(a[i+1]-a[i])
  tmp1-=(i+1)*(a[i+1]-a[i])
  tmp1+=(k-i-1)*(a[i+1]-a[i])
  ans=min(ans,tmp1-tmp0)

print(ans)
0