結果

問題 No.198 キャンディー・ボックス2
ユーザー omuomu
提出日時 2015-04-29 01:59:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 78 ms / 1,000 ms
コード長 1,867 bytes
コンパイル時間 844 ms
コンパイル使用メモリ 89,668 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 01:05:51
合計ジャッジ時間 3,238 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
6,816 KB
testcase_01 AC 73 ms
6,944 KB
testcase_02 AC 60 ms
6,940 KB
testcase_03 AC 47 ms
6,944 KB
testcase_04 AC 61 ms
6,944 KB
testcase_05 AC 78 ms
6,944 KB
testcase_06 AC 62 ms
6,944 KB
testcase_07 AC 47 ms
6,944 KB
testcase_08 AC 74 ms
6,944 KB
testcase_09 AC 26 ms
6,940 KB
testcase_10 AC 26 ms
6,944 KB
testcase_11 AC 30 ms
6,940 KB
testcase_12 AC 17 ms
6,944 KB
testcase_13 AC 78 ms
6,944 KB
testcase_14 AC 16 ms
6,944 KB
testcase_15 AC 16 ms
6,940 KB
testcase_16 AC 21 ms
6,940 KB
testcase_17 AC 25 ms
6,940 KB
testcase_18 AC 31 ms
6,940 KB
testcase_19 AC 34 ms
6,940 KB
testcase_20 AC 38 ms
6,940 KB
testcase_21 AC 43 ms
6,940 KB
testcase_22 AC 48 ms
6,944 KB
testcase_23 AC 61 ms
6,940 KB
testcase_24 AC 74 ms
6,944 KB
testcase_25 AC 53 ms
6,940 KB
testcase_26 AC 60 ms
6,940 KB
testcase_27 AC 74 ms
6,940 KB
testcase_28 AC 54 ms
6,944 KB
testcase_29 AC 75 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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