結果
| 問題 | No.50 おもちゃ箱 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2014-10-26 23:39:27 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 807 bytes | 
| コンパイル時間 | 2,961 ms | 
| コンパイル使用メモリ | 77,044 KB | 
| 実行使用メモリ | 41,428 KB | 
| 最終ジャッジ日時 | 2024-12-30 13:42:33 | 
| 合計ジャッジ時間 | 10,791 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 29 WA * 9 | 
ソースコード
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;
					nokori--;
					if(nokori==0)break;
				}
			}
			if(used)ans++;
			if(nokori==0)break;
		}
		if(nokori>0)System.out.println(-1);
		else System.out.println(ans);
	}}
            
            
            
        