結果

問題 No.198 キャンディー・ボックス2
ユーザー 👑 kmjpkmjp
提出日時 2015-04-29 00:02:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 95 ms / 1,000 ms
コード長 1,253 bytes
コンパイル時間 1,269 ms
コンパイル使用メモリ 145,688 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-10 07:27:21
合計ジャッジ時間 4,350 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
4,380 KB
testcase_01 AC 93 ms
4,376 KB
testcase_02 AC 68 ms
4,380 KB
testcase_03 AC 53 ms
4,380 KB
testcase_04 AC 68 ms
4,380 KB
testcase_05 AC 84 ms
4,376 KB
testcase_06 AC 68 ms
4,380 KB
testcase_07 AC 54 ms
4,376 KB
testcase_08 AC 85 ms
4,376 KB
testcase_09 AC 22 ms
4,380 KB
testcase_10 AC 22 ms
4,380 KB
testcase_11 AC 26 ms
4,380 KB
testcase_12 AC 23 ms
4,376 KB
testcase_13 AC 85 ms
4,380 KB
testcase_14 AC 15 ms
4,380 KB
testcase_15 AC 15 ms
4,376 KB
testcase_16 AC 19 ms
4,380 KB
testcase_17 AC 22 ms
4,380 KB
testcase_18 AC 26 ms
4,376 KB
testcase_19 AC 30 ms
4,376 KB
testcase_20 AC 61 ms
4,380 KB
testcase_21 AC 69 ms
4,376 KB
testcase_22 AC 80 ms
4,376 KB
testcase_23 AC 86 ms
4,380 KB
testcase_24 AC 50 ms
4,380 KB
testcase_25 AC 95 ms
4,380 KB
testcase_26 AC 94 ms
4,380 KB
testcase_27 AC 95 ms
4,376 KB
testcase_28 AC 95 ms
4,380 KB
testcase_29 AC 95 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

ll B;
int N;
ll C[20];
ll sum;
ll ret[140000];

ll step(ll tar) {
	ll ov=0,fl=0;
	int j;
	if(tar<0) return 1LL<<60;
	FOR(j,N) if(C[j]>tar) ov += C[j]-tar;
	FOR(j,N) if(C[j]<tar) fl += tar-C[j];
	if(fl<=ov+B) return ov+fl;
	return 1LL<<60;
}



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>B>>N;
	ll sum=B;
	FOR(i,N) cin>>C[i], sum+=C[i];
	
	
	for(i=0;i<=10000;i++) ret[i]=step(i*100000);
	ll L=0,R=0;
	for(i=0;i<=10000;i++) {
		if(ret[L]>ret[i]) L=i;
		if(ret[R]>=ret[i]) R=i;
	}
	ll M=(L+R)*100000/2;
	ll mi=1LL<<60;
	for(ll i=M-2000000;i<=M+2000000;i++) mi=min(mi,step(i));
	cout<<mi<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0