結果

問題 No.198 キャンディー・ボックス2
ユーザー kurenai3110
提出日時 2017-01-16 21:58:58
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 667 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 69,748 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-12-22 20:55:49
合計ジャッジ時間 2,320 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17 WA * 6 RE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
	long long b,n;cin>>b>>n;
	vector<long long>c(n);
	long long sum=0,ans=0;
	for(int i=0;i<n;i++){
		cin>>c[i];
		sum+=c[i];
	}
	sort(c.begin(),c.end());
	
	for(int i=0;i<n/2;i++){
		b-=(c[i+1]-c[i])*(i+1);
		ans+=(c[i+1]-c[i])*(i+1);
	}
	for(int i=n-1;i>n/2;i--){
		ans+=(c[i]-c[i-1])*(n-i);
		b+=(c[i]-c[i-1])*(n-i);
	}

	if(b<0){
		int need = ceil(-b/n);
		for(int i=0;i<n;i++){
			if(c[i]>=c[n/2])ans-=need;
			else if(c[i]<c[n/2]-need)ans-=need;
			else {
				ans-=c[n/2-c[i]];
				ans+=c[i]-(c[n/2]-need);
			}
		}
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0