結果

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

ソースコード

diff #

import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

class Main {
    public static void main(String[] args) throws IOException {
        try (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