結果

問題 No.5 数字のブロック
ユーザー tsunabittsunabit
提出日時 2018-04-15 11:48:02
言語 Java21
(openjdk 21)
結果
AC  
実行時間 302 ms / 5,000 ms
コード長 1,839 bytes
コンパイル時間 3,821 ms
コンパイル使用メモリ 76,620 KB
実行使用メモリ 47,636 KB
最終ジャッジ日時 2024-04-29 09:44:50
合計ジャッジ時間 11,834 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
41,412 KB
testcase_01 AC 135 ms
41,444 KB
testcase_02 AC 133 ms
41,072 KB
testcase_03 AC 264 ms
46,288 KB
testcase_04 AC 238 ms
45,968 KB
testcase_05 AC 279 ms
46,000 KB
testcase_06 AC 257 ms
46,288 KB
testcase_07 AC 236 ms
46,108 KB
testcase_08 AC 258 ms
46,532 KB
testcase_09 AC 210 ms
43,620 KB
testcase_10 AC 286 ms
47,336 KB
testcase_11 AC 240 ms
45,912 KB
testcase_12 AC 261 ms
46,716 KB
testcase_13 AC 269 ms
47,056 KB
testcase_14 AC 150 ms
41,704 KB
testcase_15 AC 163 ms
41,500 KB
testcase_16 AC 283 ms
47,264 KB
testcase_17 AC 296 ms
47,432 KB
testcase_18 AC 296 ms
47,484 KB
testcase_19 AC 302 ms
47,636 KB
testcase_20 AC 136 ms
41,356 KB
testcase_21 AC 136 ms
41,024 KB
testcase_22 AC 136 ms
41,176 KB
testcase_23 AC 136 ms
41,276 KB
testcase_24 AC 162 ms
41,204 KB
testcase_25 AC 187 ms
42,064 KB
testcase_26 AC 136 ms
41,196 KB
testcase_27 AC 135 ms
41,332 KB
testcase_28 AC 134 ms
41,476 KB
testcase_29 AC 243 ms
45,936 KB
testcase_30 AC 218 ms
43,440 KB
testcase_31 AC 136 ms
41,076 KB
testcase_32 AC 132 ms
41,320 KB
testcase_33 AC 135 ms
41,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

// 問題文
// Ellenは数字のブロックで遊ぼうとしている。
// 数字が書かれているブロックはそれぞれ高さ1で幅はWi である。
// (同じ幅のブロックが複数存在することがある。)
// それらのブロックを高さ1,幅Lの箱の中に入れる。 
// Ellenは大きな箱にどれだけブロックがたくさん入るか気になったが。
// 組み合わせがたくさんあって大変なことに気づいて、すぐに夜になってしまいそうである。
// あなたは、代わりに大きな箱に最大何個のブロックが入るかを求めてください。
// ***
// 入力
// L
// N
// W1W2W3…WN
// 1行目は、大きな箱の幅を表すL(1≤L≤10000)が与えられます。
// 2行目は、ブロックの数を表すN(1≤N≤10000)
// 3行目は、各ブロックの幅を表すWi(1≤Wi≤L)が半角スペース区切りで与えられます。
// ***
// 出力
// 求めた数値を返してください。末尾に改行を付けてください。

public class No5 {
    public static void main(String[] args) {
        // 標準入力から読み込む際に、Scannerオブジェクトを使う。
        Scanner sc = new Scanner(System.in);
        int L = sc.nextInt(); // 箱の幅
        int N = sc.nextInt(); // ブロックの数

        ArrayList<Integer> al = new ArrayList<Integer>();
        for(int i = 0; i < N; i++) {
            al.add(sc.nextInt());
        }
        // 並び替え
        Collections.sort(al);
        int total = 0;
        int count = 0;
        while(count < N && (total + al.get(count)) <= L) {
            total += al.get(count);
            count++;
        }
        System.out.println(count);
    }
}
0