結果

問題 No.198 キャンディー・ボックス2
ユーザー kichirb3kichirb3
提出日時 2018-03-29 14:15:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,237 bytes
コンパイル時間 709 ms
コンパイル使用メモリ 78,276 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 08:37:02
合計ジャッジ時間 1,934 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

// No.198 キャンディー・ボックス2
// https://yukicoder.me/problems/no/198
//
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <utility>
using namespace std;

long long int solve(unsigned int B, unsigned int N, vector<int>& candies);
long long calc_cost(long long  n, vector<int>& candies);


int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    unsigned int B, N;
    cin >> B >> N;
    vector<int> candies(N);
    for (int i = 0; i < N; ++i)
        cin >> candies[i];

    long long ans = solve(B, N, candies);
    cout << ans << endl;
}


long long int solve(unsigned int B, unsigned int N, vector<int>& candies) {
    sort(candies.begin(), candies.end());

    long long  median;
    if (N % 2 == 1)
        median = candies[N/2];
    else
        median = (candies[(N-1)/2] + candies[N/2]) / 2;

    long long max_avg = B;
    for (auto c: candies)
        max_avg += c;
    max_avg /= N;
    median = min(median, max_avg);

    long long ans = calc_cost(median, candies);
    return ans;
}


long long calc_cost(long long  n, vector<int>& candies) {
    long long ans = 0;
    for (auto s: candies) {
        ans += abs(s - n);
    }
    return ans;
}
0