結果

問題 No.1391 ±1 Abs Sum
ユーザー 👑 Nachia
提出日時 2021-02-12 22:14:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 570 bytes
コンパイル時間 1,828 ms
コンパイル使用メモリ 190,808 KB
最終ジャッジ日時 2025-01-18 18:44:05
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 WA * 13
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:26:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   26 |   scanf("%d%d",&N,&K);
      |   ~~~~~^~~~~~~~~~~~~~
main.cpp:27:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   27 |   rep(i,N) scanf("%lld",&A[i]);
      |            ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)



int N,K;
LL A[200000];

LL solve(){
  LL ans=0;
  LL rK=N-K;
  LL pibot;
  if(rK*2>=N) pibot=0;
  else pibot=(N-rK-rK-1)/2;
  LL x=A[pibot];
  rep(i,pibot) ans+=(x-A[i])*2;
  rep(i,K) ans+=(A[i]-x)*2;
  rep(i,N) ans+=x-A[i];
  return ans;
}

int main(){
  scanf("%d%d",&N,&K);
  rep(i,N) scanf("%lld",&A[i]);
  LL ans=solve();
  reverse(A,A+N); rep(i,N) A[i]=-A[i];
  ans=min(ans,solve());
  printf("%lld\n",ans);
  return 0;
}

0