結果
| 問題 | No.50 おもちゃ箱 | 
| コンテスト | |
| ユーザー |  kou6839 | 
| 提出日時 | 2014-12-19 16:29:45 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,217 bytes | 
| コンパイル時間 | 2,380 ms | 
| コンパイル使用メモリ | 77,712 KB | 
| 実行使用メモリ | 54,792 KB | 
| 最終ジャッジ日時 | 2024-06-12 01:49:08 | 
| 合計ジャッジ時間 | 9,889 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 WA * 1 | 
| other | AC * 29 WA * 9 | 
ソースコード
import java.math.*;
import java.util.*;
 
public class Main {
	static int dfs(int n,int[] omo,int[] hako,int[] use){
		if(n==((1<<omo.length)-1)){
			int ans=0;
			for(int i=0;i<use.length;i++){
				if(use[i]>0) ans++;
			}
			return ans;
		}
		int res=Integer.MAX_VALUE;
		for(int i=0;i<omo.length;i++){
			if((n&1<<i)!=0) continue;
			int doko=-1;
			for(int j=hako.length-1;j>=0;j--){
				if(omo[i]<=hako[j]){
					hako[j]-=omo[i];
					use[j]++;
					doko=j;
					break;
				}else{
					if(j==0) return 100000;
				}
			}
			n += 1<<i;
			res = Math.min(dfs(n,omo,hako,use),res);
			n-=1<<i;
			use[doko]--;
			hako[doko]+=omo[i];
		}
		return res;
	}
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n= sc.nextInt();
        int[] omo = new int[n];
        for(int i=0;i<n;i++){
        	omo[i]=sc.nextInt();
        }
        int m = sc.nextInt();
        int[] hako =new int[m];
        int[] use = new int[m];
        for(int i=0;i<m;i++){
        	hako[i]=sc.nextInt();
        }
        Arrays.sort(hako);
        int s=dfs(0,omo,hako,use);
        if(s==100000){
        	System.out.println(-1);
        }
        System.out.println(s);
        
    }
}
            
            
            
        