結果

問題 No.198 キャンディー・ボックス2
ユーザー omu
提出日時 2015-04-29 01:59:58
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 74 ms / 1,000 ms
コード長 1,867 bytes
コンパイル時間 871 ms
コンパイル使用メモリ 89,316 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-16 09:00:43
合計ジャッジ時間 3,623 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <iomanip>
#include <list>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }
template<class T> inline T sqr(T x) { return x*x; }
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

#define For(i,a,b)	for(int (i) = (a);i < (b);(i)++)
#define rep(i,n)	For(i,0,n)
#define clr(a)		memset((a), 0 ,sizeof(a))
#define mclr(a)		memset((a), -1 ,sizeof(a))
#define all(a)		(a).begin(),(a).end()
#define rall(a)		(a).rbegin(), (a).rend()
#define sz(a)		(sizeof(a))
#define Fill(a,v)	fill((int*)a,(int*)(a+(sz(a)/sz(*(a)))),v)

bool cheak(int x, int y, int xMax, int yMax){ return x >= 0 && y >= 0 && xMax > x && yMax > y; }
const int dx[9] = { -1, -1, -1, 0 , 0 , 0 , 1 , 1 , 1}, dy[9] = { -1, 0, 1 ,-1 , 0 , 1, -1 , 0,  1};
const int mod = 1000000007;
const ll INF = 1e15;

ll c[11];
ll b, n;
ll ave = 0;
ll target;

ll ok(){
	ll m = b;
	ll ret = 0;
	rep(i, n){
		if (c[i] > target){
			m += c[i] - target;
			ret += c[i] - target;
		}
	}
	rep(i, n){
		if (c[i] < target){
			m -= target - c[i];
			if (m < 0)return INF;
			ret += target - c[i];
		}
	}
	return ret;
}

int main()
{
	cin >> b;
	cin >> n;

	ll sum = 0;
	rep(i, n){
		cin >> c[i];
		sum += c[i];
	}

	int mid;
	sort(c, c + n);
	int ave = (sum+b)/n;
	mid = c[n / 2];
	ll ans = INF;
	for (target = mid - 1e6; target <= mid + 1e6; target++){
		ans = min(ans, ok());
	}
	for (target = ave - 1e6; target <= ave + 1e6; target++){
		ans = min(ans, ok());
	}

	cout << ans << endl;

	return 0;
}
0