結果

問題 No.1391 ±1 Abs Sum
ユーザー どららどらら
提出日時 2021-02-12 22:40:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 752 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 171,892 KB
実行使用メモリ 7,816 KB
最終ジャッジ日時 2023-09-27 05:31:06
合計ジャッジ時間 5,258 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 WA -
testcase_06 AC 2 ms
4,380 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 54 ms
5,376 KB
testcase_15 AC 58 ms
5,284 KB
testcase_16 AC 71 ms
6,916 KB
testcase_17 AC 67 ms
6,764 KB
testcase_18 AC 89 ms
7,296 KB
testcase_19 WA -
testcase_20 WA -
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 98 ms
7,772 KB
testcase_32 AC 48 ms
5,188 KB
testcase_33 AC 56 ms
5,296 KB
testcase_34 AC 95 ms
7,124 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 95 ms
7,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

ll f(ll x, vector<int>& v, int K){
  vector<ll> ret;
  rep(i,v.size()){
    ret.push_back(abs(v[i]-x));
  }
  sort(ALLOF(ret));
  ll sum = 0;
  rep(i,v.size()){
    if(i<K) sum += ret[i];
    else sum += -ret[i];
  }
  return sum;
}

int main(){
  int N, K;
  cin >> N >> K;
  vector<int> v(N);
  rep(i,N) cin >> v[i];

  ll ret = f(v[N/2],v,K);
  ret = min(ret, f(v[0],v,K));
  ret = min(ret, f(v[N-1],v,K));

  cout << ret << endl;
  
  return 0;
}
0