結果

問題 No.2028 Even Choice
ユーザー tentententen
提出日時 2023-08-08 15:20:55
言語 Java21
(openjdk 21)
結果
AC  
実行時間 426 ms / 2,000 ms
コード長 2,061 bytes
コンパイル時間 4,383 ms
コンパイル使用メモリ 84,528 KB
実行使用メモリ 65,048 KB
最終ジャッジ日時 2024-04-26 07:58:19
合計ジャッジ時間 12,459 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 421 ms
65,048 KB
testcase_01 AC 409 ms
65,016 KB
testcase_02 AC 426 ms
63,412 KB
testcase_03 AC 304 ms
64,400 KB
testcase_04 AC 314 ms
64,708 KB
testcase_05 AC 214 ms
56,340 KB
testcase_06 AC 369 ms
62,024 KB
testcase_07 AC 199 ms
55,944 KB
testcase_08 AC 318 ms
58,220 KB
testcase_09 AC 332 ms
58,340 KB
testcase_10 AC 191 ms
55,636 KB
testcase_11 AC 198 ms
56,100 KB
testcase_12 AC 140 ms
52,788 KB
testcase_13 AC 276 ms
60,348 KB
testcase_14 AC 231 ms
56,076 KB
testcase_15 AC 49 ms
50,508 KB
testcase_16 AC 48 ms
50,080 KB
testcase_17 AC 51 ms
50,444 KB
testcase_18 AC 54 ms
50,592 KB
testcase_19 AC 56 ms
50,440 KB
testcase_20 AC 64 ms
50,688 KB
testcase_21 AC 64 ms
50,696 KB
testcase_22 AC 56 ms
50,088 KB
testcase_23 AC 52 ms
50,256 KB
testcase_24 AC 51 ms
50,500 KB
testcase_25 AC 51 ms
50,320 KB
testcase_26 AC 53 ms
50,160 KB
testcase_27 AC 49 ms
50,504 KB
testcase_28 AC 255 ms
57,944 KB
testcase_29 AC 211 ms
58,116 KB
testcase_30 AC 220 ms
55,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
        }
        PriorityQueue<Integer> stock = new PriorityQueue<>();
        long total = 0;
        long ans = 0;
        for (int i = n - 1; i >= 0; i--) {
            if (i % 2 == 1) {
                while (stock.size() >= k) {
                    total -= stock.poll();
                }
                ans = Math.max(ans, total + values[i]);
            }
            total += values[i];
            stock.add(values[i]);
        }
        System.out.println(ans);
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0