結果
| 問題 | No.198 キャンディー・ボックス2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-12-08 02:06:42 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 1,011 bytes |
| コンパイル時間 | 1,221 ms |
| コンパイル使用メモリ | 165,248 KB |
| 実行使用メモリ | 7,720 KB |
| 最終ジャッジ日時 | 2025-11-26 11:43:29 |
| 合計ジャッジ時間 | 2,328 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 27 |
ソースコード
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;
template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}
int main() {
ll b, n; cin >> b >> n;
ll sum = b;
vector<ll> c(n);
REP(i, n) {
cin >> c[i];
sum += c[i];
}
auto f = [&](ll x) {
ll cost = 0;
REP(i, n) cost += abs(c[i] - x);
return cost;
};
ll l = 0, r = sum / n + 1;
while (r - l > 3) {
ll t1 = (2 * l + r) / 3, t2 = (l + r * 2) / 3;
if (f(t1) >= f(t2)) {
l = t1;
} else {
r = t2;
}
}
ll ans = numeric_limits<ll>::max();
FOR(i, l, r) ans = min(ans, f(i));
cout << ans << endl;
return 0;
}