結果

問題 No.5 数字のブロック
ユーザー koukonko
提出日時 2015-12-16 21:26:03
言語 Java
(openjdk 23)
結果
AC  
実行時間 379 ms / 5,000 ms
コード長 887 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 77,572 KB
実行使用メモリ 52,668 KB
最終ジャッジ日時 2024-11-18 07:58:26
合計ジャッジ時間 7,517 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) {
		BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

		try {
			int L = Integer.parseInt(read.readLine());
			int N = Integer.parseInt(read.readLine());

			String[] box = read.readLine().split(" ");
			int[] w = new int[N];
			for (int i = 0; i < N; ++i) {
				w[i] = Integer.parseInt(box[i]);
			}

			int total = 0, ans = 0;
			for (int i = 0; i < N - 1; ++i) {
				for (int j = i + 1; j < N; ++j) {
					if (w[i] > w[j]) {
						int sw = w[i];
						w[i] = w[j];
						w[j] = sw;
					}
				}
			}
			for (int i = 0; i < N; ++i) {
				total += w[i];
				if (total <= L) {
					++ans;
				} else {
					break;
				}

			}
			System.out.println(ans);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
0