結果

問題 No.198 キャンディー・ボックス2
ユーザー dnish
提出日時 2017-07-11 00:57:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 647 bytes
コンパイル時間 1,634 ms
コンパイル使用メモリ 170,232 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-16 10:13:10
合計ジャッジ時間 2,437 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i,n,N) for(ll i=(n);i<(ll) N;i++)
#define p(s) cout<<(s)<<endl
typedef long long ll;
using namespace std;
const ll inf=1e15;

ll C[10];
ll B,N;
ll search(ll mid){
	ll cnt = 0;
	REP(i,0,N){
		cnt+=abs(mid - C[i]);
	}
	return cnt;
}
int main() {
	cin>>B>>N;
	ll sum=B;
	REP(i,0,N) {
		cin>>C[i];
		sum+=C[i];
	}
	if(N==1){
		p(0);
		return 0;;
	}
	sort(C,C+N);
	ll lb=C[0];
	ll ub=sum/N+1;
	while(ub -lb > 3){
		ll mid1=(lb * 2 + ub) / 3;
		ll mid2=(lb + ub * 2) / 3;
		if(search(mid1) > search(mid2)) lb=mid1;
		else ub = mid2;
	}
	ll ans = inf;
	REP(i,lb,ub){
		ans=min(ans,search(i));
	}
	p(ans);
	return 0;
}
0