結果

問題 No.1046 Fruits Rush
ユーザー ks2mks2m
提出日時 2020-05-08 21:26:28
言語 Java21
(openjdk 21)
結果
AC  
実行時間 124 ms / 2,000 ms
コード長 476 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 72,584 KB
実行使用メモリ 56,608 KB
最終ジャッジ日時 2023-09-17 01:30:45
合計ジャッジ時間 5,140 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
55,860 KB
testcase_01 AC 109 ms
56,076 KB
testcase_02 AC 110 ms
56,144 KB
testcase_03 AC 113 ms
56,128 KB
testcase_04 AC 119 ms
56,044 KB
testcase_05 AC 116 ms
56,336 KB
testcase_06 AC 117 ms
55,868 KB
testcase_07 AC 119 ms
56,120 KB
testcase_08 AC 113 ms
56,168 KB
testcase_09 AC 118 ms
56,608 KB
testcase_10 AC 108 ms
55,956 KB
testcase_11 AC 111 ms
55,808 KB
testcase_12 AC 122 ms
55,980 KB
testcase_13 AC 124 ms
56,524 KB
testcase_14 AC 116 ms
56,232 KB
testcase_15 AC 116 ms
55,996 KB
testcase_16 AC 117 ms
56,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();

		Arrays.sort(a);
		int ans = a[n - 1];
		for (int i = n - 2; i >= n - k; i--) {
			if (a[i] >= 0) {
				ans += a[i];
			}
		}
		System.out.println(ans);
	}
}
0