結果

問題 No.50 おもちゃ箱
ユーザー ttkkggww
提出日時 2022-04-02 03:17:59
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 250 ms / 5,000 ms
コード長 933 bytes
コンパイル時間 3,879 ms
コンパイル使用メモリ 253,732 KB
最終ジャッジ日時 2025-01-28 14:28:25
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int n,m;
vector<int> a,b;

void solve(){
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	const int INF = INT_MAX/2;
	int ans = INF;
	do{
		auto A = a;
		auto B = b;
		bool ok = true;
		int cnt = 0;
		bool cntis = true;
		for(int i = 0;i<n;i++){
			if(B.empty()){
				ok = false;
				break;
			}else{
				while(B.size()&&B.back()-A[i]<0){
					B.pop_back();
					cntis = true;
				}
				if(B.empty()){
					ok = false;
					break;
				}
				B.back()-= A[i];
				if(cntis)cnt++;
				cntis = false;
			}
		}
		if(ok)
		ans = min(ans,cnt);
	}while(next_permutation(a.begin(),a.end()));
	if(ans==INF)ans=-1;
	cout<<ans<<endl;
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n;
	a = vector<int>(n);
	for(auto &i:a)cin >> i;
	cin >> m;
	b = vector<int>(m);
	for(auto &i:b)cin >> i;
	solve();
}
0