結果

問題 No.50 おもちゃ箱
ユーザー kou6839kou6839
提出日時 2014-12-19 16:33:09
言語 Java21
(openjdk 21)
結果
AC  
実行時間 406 ms / 5,000 ms
コード長 1,236 bytes
コンパイル時間 2,610 ms
コンパイル使用メモリ 74,404 KB
実行使用メモリ 58,772 KB
最終ジャッジ日時 2023-09-08 03:36:12
合計ジャッジ時間 11,393 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
55,800 KB
testcase_01 AC 329 ms
56,360 KB
testcase_02 AC 122 ms
55,832 KB
testcase_03 AC 118 ms
56,012 KB
testcase_04 AC 121 ms
55,828 KB
testcase_05 AC 315 ms
56,112 KB
testcase_06 AC 157 ms
56,096 KB
testcase_07 AC 120 ms
55,972 KB
testcase_08 AC 120 ms
55,960 KB
testcase_09 AC 119 ms
55,928 KB
testcase_10 AC 122 ms
55,736 KB
testcase_11 AC 123 ms
55,648 KB
testcase_12 AC 123 ms
55,628 KB
testcase_13 AC 122 ms
55,956 KB
testcase_14 AC 123 ms
56,148 KB
testcase_15 AC 130 ms
55,572 KB
testcase_16 AC 126 ms
55,768 KB
testcase_17 AC 125 ms
56,360 KB
testcase_18 AC 125 ms
53,992 KB
testcase_19 AC 256 ms
56,616 KB
testcase_20 AC 122 ms
55,988 KB
testcase_21 AC 138 ms
56,960 KB
testcase_22 AC 142 ms
56,448 KB
testcase_23 AC 122 ms
56,116 KB
testcase_24 AC 120 ms
55,612 KB
testcase_25 AC 120 ms
56,164 KB
testcase_26 AC 133 ms
55,804 KB
testcase_27 AC 169 ms
56,232 KB
testcase_28 AC 166 ms
56,528 KB
testcase_29 AC 149 ms
56,408 KB
testcase_30 AC 132 ms
56,204 KB
testcase_31 AC 129 ms
55,856 KB
testcase_32 AC 199 ms
56,364 KB
testcase_33 AC 234 ms
56,836 KB
testcase_34 AC 309 ms
56,420 KB
testcase_35 AC 167 ms
56,352 KB
testcase_36 AC 265 ms
56,624 KB
testcase_37 AC 170 ms
56,532 KB
testcase_38 AC 143 ms
56,140 KB
testcase_39 AC 294 ms
56,240 KB
testcase_40 AC 406 ms
56,096 KB
testcase_41 AC 226 ms
58,772 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);
        	return;
        }
        System.out.println(s);
        
    }
}		
0