結果

問題 No.1046 Fruits Rush
ユーザー tentententen
提出日時 2020-11-09 20:25:04
言語 Java21
(openjdk 21)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 603 bytes
コンパイル時間 1,994 ms
コンパイル使用メモリ 71,732 KB
実行使用メモリ 57,960 KB
最終ジャッジ日時 2023-09-29 22:14:56
合計ジャッジ時間 4,451 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
56,100 KB
testcase_01 AC 108 ms
56,424 KB
testcase_02 AC 109 ms
57,960 KB
testcase_03 AC 115 ms
55,992 KB
testcase_04 AC 116 ms
55,944 KB
testcase_05 AC 120 ms
56,048 KB
testcase_06 AC 110 ms
56,124 KB
testcase_07 AC 118 ms
56,268 KB
testcase_08 AC 114 ms
56,028 KB
testcase_09 AC 119 ms
56,644 KB
testcase_10 AC 109 ms
55,744 KB
testcase_11 AC 107 ms
55,980 KB
testcase_12 AC 120 ms
55,944 KB
testcase_13 AC 118 ms
56,264 KB
testcase_14 AC 109 ms
55,828 KB
testcase_15 AC 108 ms
56,088 KB
testcase_16 AC 109 ms
55,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        Arrays.sort(arr);
        if (arr[n - 1] < 0) {
            System.out.println(arr[n - 1]);
            return;
        }
        long ans = 0;
        for (int i = 0; i < k && arr[n - 1 - i] > 0; i++) {
            ans += arr[n - 1 - i];
        }
        System.out.println(ans);
    }
}
0