結果

問題 No.50 おもちゃ箱
ユーザー リチウム
提出日時 2014-10-27 00:01:08
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 793 bytes
コンパイル時間 3,129 ms
コンパイル使用メモリ 77,072 KB
実行使用メモリ 41,680 KB
最終ジャッジ日時 2024-12-30 13:44:21
合計ジャッジ時間 10,730 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 29 WA * 9
権限があれば一括ダウンロードができます

ソースコード

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(toy);
		Arrays.sort(box);
		int nokori=n;
		int ans=0;
		boolean t[]=new boolean[n];
		for(int i=m-1;i>=0;i--){
			int x=box[i];
			boolean used=false;
			for(int j=n-1;j>=0;j--){
				if(!t[j] && x>=toy[j]){
					t[j]=true;
					x-=toy[j];
	                used=true;
				}
			}
			if(used)ans++;
		}
		for(int i=0;i<n;i++){
			if(!t[i]){
				System.out.println(-1);
				return;
			}
		}
	    System.out.println(ans);
	}}
0