結果

問題 No.50 おもちゃ箱
ユーザー Kutimoti_T
提出日時 2018-01-03 16:53:19
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 97 ms / 5,000 ms
コード長 837 bytes
コンパイル時間 583 ms
コンパイル使用メモリ 60,120 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 22:05:40
合計ジャッジ時間 2,264 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define FOR(i,s,e) for(int i = (s);i <= (e);i++)

int N;
int A[10];

int M;
int B[10];


int main()
{
	cin >> N;
	FOR(i,0,N - 1)
	{
		cin >> A[i];
	}
	cin >> M;
	FOR(i,0,M - 1)
	{
		cin >> B[i];
	}


	sort(B,B + M);
	reverse(B,B + M);
	sort(A,A+N);
	int result = 11;
	do
	{
		int box[M];
		FOR(i,0,M - 1) box[i] = B[i];
		int cou = 0;
		bool success = true;
		FOR(i,0,N -1)
		{
			bool update = false;
			FOR(j,0,M - 1)
			{
				if(box[j] - A[i] >= 0)
				{
					box[j] -= A[i];
					cou = max(cou,j + 1);
					update = true;

					break;
				}
			}
			if(update == false)
			{
				success = false;
				break;
			}
		}
		if(success)
		{
			result = min(result,cou);
		}
	}while(next_permutation(A,A + N));
if(result == 11) result = -1;
	cout << result << endl;
	return 0;
}
0