結果
| 問題 | 
                            No.1391 ±1 Abs Sum
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Nachia
                         | 
                    
| 提出日時 | 2021-02-12 22:08:41 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 608 bytes | 
| コンパイル時間 | 2,047 ms | 
| コンパイル使用メモリ | 192,364 KB | 
| 最終ジャッジ日時 | 2025-01-18 18:40:07 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 13 WA * 21 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:27:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   27 |   scanf("%d%d",&N,&K);
      |   ~~~~~^~~~~~~~~~~~~~
main.cpp:28:17: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   28 |   rep(i,N) scanf("%lld",&A[i]);
      |            ~~~~~^~~~~~~~~~~~~~
            
            ソースコード
#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;
  if(K*2<=N){
    rep(i,K) ans+=(A[i]-A[0])*2;
    rep(i,N) ans+=A[0]-A[i];
    return ans;
  }
  LL pibot = (N-K-K-1)/2;
  rep(i,pibot) ans+=(A[0]-A[i])*2;
  rep(i,K) ans+=(A[i]-A[0])*2;
  rep(i,N) ans+=A[0]-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;
}
            
            
            
        
            
Nachia