結果

問題 No.50 おもちゃ箱
ユーザー kou6839kou6839
提出日時 2014-12-19 16:29:45
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,217 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 74,520 KB
実行使用メモリ 57,180 KB
最終ジャッジ日時 2023-09-02 19:24:45
合計ジャッジ時間 10,951 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
56,400 KB
testcase_01 AC 339 ms
55,812 KB
testcase_02 AC 123 ms
55,828 KB
testcase_03 AC 122 ms
55,708 KB
testcase_04 WA -
testcase_05 AC 325 ms
56,648 KB
testcase_06 AC 162 ms
56,104 KB
testcase_07 AC 125 ms
55,656 KB
testcase_08 AC 125 ms
56,040 KB
testcase_09 AC 125 ms
56,040 KB
testcase_10 AC 124 ms
56,004 KB
testcase_11 AC 125 ms
55,496 KB
testcase_12 AC 126 ms
55,620 KB
testcase_13 AC 125 ms
55,500 KB
testcase_14 AC 126 ms
55,820 KB
testcase_15 AC 126 ms
55,480 KB
testcase_16 AC 125 ms
55,808 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 264 ms
55,964 KB
testcase_20 AC 128 ms
55,624 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 135 ms
55,984 KB
testcase_27 AC 169 ms
56,468 KB
testcase_28 AC 164 ms
55,996 KB
testcase_29 WA -
testcase_30 AC 123 ms
55,632 KB
testcase_31 AC 128 ms
55,720 KB
testcase_32 AC 202 ms
56,268 KB
testcase_33 AC 229 ms
56,128 KB
testcase_34 AC 300 ms
55,724 KB
testcase_35 AC 157 ms
56,172 KB
testcase_36 AC 257 ms
56,072 KB
testcase_37 AC 158 ms
56,544 KB
testcase_38 WA -
testcase_39 AC 293 ms
56,148 KB
testcase_40 AC 413 ms
57,180 KB
testcase_41 AC 226 ms
56,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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);
        
    }
}
0