結果
| 問題 | No.50 おもちゃ箱 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-06-07 08:58:00 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 91 ms / 5,000 ms | 
| コード長 | 563 bytes | 
| コンパイル時間 | 2,499 ms | 
| コンパイル使用メモリ | 194,560 KB | 
| 最終ジャッジ日時 | 2025-01-10 23:41:12 | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 38 | 
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:29:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   29 |         scanf("%d",&n);
      |         ~~~~~^~~~~~~~~
main.cpp:30:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   30 |         rep(i,n) scanf("%d",&a[i]);
      |                  ~~~~~^~~~~~~~~~~~
main.cpp:31:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   31 |         scanf("%d",&m);
      |         ~~~~~^~~~~~~~~
main.cpp:32:23: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |         rep(j,m) scanf("%d",&b[j]);
      |                  ~~~~~^~~~~~~~~~~~
            
            ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int INF=1<<29;
int n,m,a[10],b[10];
int used[10],ans;
void dfs(int i,int cnt){
	if(cnt>=ans) return;
	if(i==n){
		ans=cnt;
		return;
	}
	rep(j,m) if(used[j]+a[i]<=b[j]) {
		used[j]+=a[i];
		dfs(i+1,cnt+(used[j]==a[i]?1:0));
		used[j]-=a[i];
	}
}
int main(){
	scanf("%d",&n);
	rep(i,n) scanf("%d",&a[i]);
	scanf("%d",&m);
	rep(j,m) scanf("%d",&b[j]);
	sort(a,a+n,greater<int>());
	sort(b,b+m);
	ans=INF;
	dfs(0,0);
	printf("%d\n",ans<INF?ans:-1);
	return 0;
}
            
            
            
        