結果

問題 No.5 数字のブロック
ユーザー vjudge1
提出日時 2025-03-06 14:18:38
言語 Java
(openjdk 23)
結果
AC  
実行時間 273 ms / 5,000 ms
コード長 713 bytes
コンパイル時間 2,310 ms
コンパイル使用メモリ 82,180 KB
実行使用メモリ 59,840 KB
最終ジャッジ日時 2025-03-06 14:18:49
合計ジャッジ時間 10,008 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Read input
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = scanner.nextInt();
        }

        // Sort the array
        Arrays.sort(a);

        // Iterate through the sorted array
        for (int i = 0; i < n; i++) {
            k -= a[i];
            if (k < 0) {
                System.out.println(i);
                return;
            }
        }

        // If all elements are used
        System.out.println(n);
    }
}
0