結果

問題 No.50 おもちゃ箱
ユーザー リチウムリチウム
提出日時 2014-10-27 01:09:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,251 ms / 5,000 ms
コード長 1,461 bytes
コンパイル時間 2,116 ms
コンパイル使用メモリ 76,488 KB
実行使用メモリ 60,096 KB
最終ジャッジ日時 2023-09-08 03:42:15
合計ジャッジ時間 46,069 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
56,068 KB
testcase_01 AC 2,092 ms
59,432 KB
testcase_02 AC 132 ms
56,024 KB
testcase_03 AC 125 ms
55,660 KB
testcase_04 AC 127 ms
55,948 KB
testcase_05 AC 1,857 ms
59,136 KB
testcase_06 AC 426 ms
59,848 KB
testcase_07 AC 125 ms
56,204 KB
testcase_08 AC 127 ms
55,848 KB
testcase_09 AC 127 ms
55,916 KB
testcase_10 AC 124 ms
55,528 KB
testcase_11 AC 129 ms
55,912 KB
testcase_12 AC 126 ms
56,148 KB
testcase_13 AC 123 ms
55,760 KB
testcase_14 AC 132 ms
56,068 KB
testcase_15 AC 128 ms
55,960 KB
testcase_16 AC 131 ms
55,784 KB
testcase_17 AC 2,053 ms
58,964 KB
testcase_18 AC 386 ms
59,596 KB
testcase_19 AC 2,077 ms
58,696 KB
testcase_20 AC 165 ms
57,272 KB
testcase_21 AC 374 ms
60,072 KB
testcase_22 AC 2,176 ms
58,844 KB
testcase_23 AC 360 ms
60,096 KB
testcase_24 AC 127 ms
55,680 KB
testcase_25 AC 125 ms
56,088 KB
testcase_26 AC 164 ms
57,472 KB
testcase_27 AC 380 ms
60,024 KB
testcase_28 AC 2,081 ms
59,688 KB
testcase_29 AC 2,104 ms
58,928 KB
testcase_30 AC 2,251 ms
59,552 KB
testcase_31 AC 125 ms
55,948 KB
testcase_32 AC 2,139 ms
59,544 KB
testcase_33 AC 2,068 ms
59,208 KB
testcase_34 AC 1,930 ms
59,380 KB
testcase_35 AC 2,094 ms
59,212 KB
testcase_36 AC 2,017 ms
59,372 KB
testcase_37 AC 2,115 ms
59,104 KB
testcase_38 AC 2,148 ms
59,056 KB
testcase_39 AC 1,945 ms
59,316 KB
testcase_40 AC 2,016 ms
58,904 KB
testcase_41 AC 2,002 ms
58,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int toy[]=new int[n];
		for(int i=0;i<n;i++){
			toy[i]=sc.nextInt();
		}
		int m=sc.nextInt();
		int box[]=new int[m];
		for(int i=0;i<m;i++){
			box[i]=sc.nextInt();
		}
		Arrays.sort(box);
		int x=1;
		int y=n;
		while(y>1){
			x*=y;
			y--;
		}
		int ans=100;
		for(int i=0;i<x;i++){
			int p[]=permutation(toy,i,x);
			boolean used[]=new boolean[m];
			int b[]=new int[m];
			for(int j=0;j<m;j++){
				b[j]=box[j];
			}
			int nokori=n;
			for(int j=0;j<n;j++){
				for(int k=m-1;k>=0;k--){
					if(b[k]>=p[j]){
						b[k]-=p[j];
						used[k]=true;
						nokori--;
						break;
					}
				}
		}
			int ak=0;
			for(int j=0;j<m;j++){
				if(b[j]!=box[j])ak++;
			}
			if(nokori==0)ans=Math.min(ans,ak);
		}
		if(ans==100)System.out.println(-1);
		else System.out.println(ans);
	}
	
	static int[] permutation(int[] base, int x,int k) {
		int n = base.length;
		int ans[] = new int[n];
		if (n == 1)
			return base;
		else {
			ArrayList<Integer> B = new ArrayList<Integer>();
			for (int i = 0; i < n; i++) {
				B.add(base[i]);
			}
			k /= n;
			int N = n - 1;
			for (int i = 0; i < n - 1; i++) {
				ans[i] = B.get(x / k);
				B.remove(x / k);
				x %= k;
				k /= N;
				N--;
			}
			ans[n - 1] = B.get(0);
		}
		return ans;
	}
}
0