結果

問題 No.198 キャンディー・ボックス2
ユーザー kk
提出日時 2020-08-07 07:33:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 614 bytes
コンパイル時間 2,265 ms
コンパイル使用メモリ 207,044 KB
実行使用メモリ 4,368 KB
最終ジャッジ日時 2023-10-23 21:09:37
合計ジャッジ時間 3,907 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 WA -
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 1 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 WA -
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)

long long calc(const vector<long long> &v, long long t) {
  long long ret = 0;
  for (long long x: v)
    ret += abs(x - t);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  long long b;

  cin >> b >> n;
  vector<long long>c(n);
  REP (i, n) cin >> c[i];

  sort(c.begin(), c.end());
  long long sum = 0;
  REP (i, n) sum += c[i];
  
  long long ret = calc(c, (sum + b) / n);
  if (sum >= n * c[n/2])
    ret = min(ret, calc(c, c[n/2]));
  cout << ret << endl;
  
  return 0;
}
0