結果
| 問題 | 
                            No.198 キャンディー・ボックス2
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2019-11-09 00:40:02 | 
| 言語 | C++11(廃止可能性あり)  (gcc 13.3.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 2 ms / 1,000 ms | 
| コード長 | 1,289 bytes | 
| コンパイル時間 | 2,144 ms | 
| コンパイル使用メモリ | 157,672 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-09-15 03:25:46 | 
| 合計ジャッジ時間 | 3,155 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;
const ll INF = 1e11;
ll B;
ll N;
ll C[20];
ll solve(ll x){
    ll score = 0;
    ll b = B;
    REP(i,N){
        ll c = C[i];
        if(c - x > 0){
            b += c-x;
            score += c-x;
        }
    }
    REP(i,N){
        ll c = C[i];
        if(x - c > 0){
            b -= x-c;
            score += x-c;
        }
    }
    if(b < 0) score += INF;
    return score;
}
int main(){
    cin >> B;
    cin >> N;
    REP(i,N){
        ll c;
        cin >> c;
        C[i] = c;
    }
    ll left = 0;
    ll right = 1e10;
    ll ans = 0;
    REP(i,100){
        ll x1 = (left*2 + right) / 3;
        ll x2 = (left + right*2) / 3;
        ll s1 = solve(x1);
        ll s2 = solve(x2);
        if(s1 < s2){
            right = x2;
            ans = s1;
        }
        else{
            left = x1;
            ans = s2;
        }
    }
    cout << ans << endl;
    return 0;
}