結果

問題 No.198 キャンディー・ボックス2
ユーザー 👑 kmjpkmjp
提出日時 2015-04-28 23:58:24
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,242 bytes
コンパイル時間 1,414 ms
コンパイル使用メモリ 145,636 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-18 14:21:15
合計ジャッジ時間 2,560 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 7 ms
4,380 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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);
	int 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)/2;
	ll mi=1LL<<60;
	for(i=M-200000;i<=M+200000;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