結果

問題 No.198 キャンディー・ボックス2
ユーザー prtoq3prtoq3
提出日時 2018-10-28 19:30:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 743 bytes
コンパイル時間 1,440 ms
コンパイル使用メモリ 144,252 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-12 08:08:38
合計ジャッジ時間 2,737 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 WA -
testcase_20 AC 1 ms
4,376 KB
testcase_21 WA -
testcase_22 AC 2 ms
4,376 KB
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 1 ms
4,380 KB
testcase_28 WA -
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘ll func(int)’:
main.cpp:21:30: warning: left shift count >= width of type [-Wshift-count-overflow]
     if (stock < 0) return 1<<60;
                              ^~

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const int INF = 1000000000;

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

int n, b;
int a[20];

ll func(int x){
    ll ret = 0;
    ll stock = b;
    for (int i = 0; i < n; i++){
        if (a[i] < x) stock -= (x - a[i]);
        else stock += (a[i] - x);
        ret += abs(a[i] - x);
    }

    if (stock < 0) return 1<<60;
    return ret;
}

int main(){
    cin >> b >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    int ng = -1;
    int ok = INF;
    while(abs(ok - ng) > 1){
        int mid = (ok + ng) / 2;
        if (func(mid) <= func(mid + 1)) ok = mid;
        else ng = mid;
    }

    cout << func(ok) << endl;

    return 0;
}
0