結果

問題 No.2139 K Consecutive Sushi
ユーザー tentententen
提出日時 2023-07-06 08:39:44
言語 Java21
(openjdk 21)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 2,813 bytes
コンパイル時間 2,600 ms
コンパイル使用メモリ 90,504 KB
実行使用メモリ 71,264 KB
最終ジャッジ日時 2023-09-27 09:43:42
合計ジャッジ時間 16,039 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,680 KB
testcase_01 AC 44 ms
48,124 KB
testcase_02 AC 45 ms
49,684 KB
testcase_03 AC 678 ms
71,264 KB
testcase_04 AC 655 ms
68,820 KB
testcase_05 AC 664 ms
70,468 KB
testcase_06 AC 624 ms
70,804 KB
testcase_07 AC 692 ms
70,320 KB
testcase_08 AC 470 ms
65,352 KB
testcase_09 AC 492 ms
67,496 KB
testcase_10 AC 510 ms
67,076 KB
testcase_11 AC 494 ms
66,280 KB
testcase_12 AC 474 ms
66,620 KB
testcase_13 AC 44 ms
50,000 KB
testcase_14 AC 45 ms
49,588 KB
testcase_15 AC 44 ms
49,680 KB
testcase_16 AC 45 ms
49,544 KB
testcase_17 AC 44 ms
49,784 KB
testcase_18 AC 83 ms
52,224 KB
testcase_19 AC 74 ms
51,948 KB
testcase_20 AC 74 ms
50,952 KB
testcase_21 AC 48 ms
49,584 KB
testcase_22 AC 90 ms
51,924 KB
testcase_23 AC 163 ms
53,684 KB
testcase_24 AC 338 ms
57,544 KB
testcase_25 AC 433 ms
63,296 KB
testcase_26 AC 180 ms
56,704 KB
testcase_27 AC 304 ms
59,876 KB
testcase_28 AC 304 ms
58,932 KB
testcase_29 AC 219 ms
57,512 KB
testcase_30 AC 360 ms
61,364 KB
testcase_31 AC 661 ms
68,812 KB
testcase_32 AC 229 ms
57,380 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();
        long total = 0;
        int[] values = new int[n + 2];
        for (int i = 1; i <= n; i++) {
            values[i] = sc.nextInt();
            total += values[i];
        }
        Sushi[] sushies = new Sushi[n + 2];
        sushies[0] = new Sushi(0, 0);
        TreeSet<Sushi> mins = new TreeSet<>();
        mins.add(sushies[0]);
        for (int i = 1; i <= n + 1; i++) {
            sushies[i] = new Sushi(i, mins.first().value + values[i]);
            mins.add(sushies[i]);
            if (i - k >= 0) {
                mins.remove(sushies[i - k]);
            }
        }
        System.out.println(total - sushies[n + 1].value);
    }
    
    static class Sushi implements Comparable<Sushi> {
        int idx;
        long value;
        
        public Sushi(int idx, long value) {
            this.idx = idx;
            this.value = value;
        }
        
        public int hashCode() {
            return idx;
        }
        
        public int compareTo(Sushi another) {
            if (value == another.value) {
                return idx - another.idx;
            } else if (value < another.value) {
                return -1;
            } else {
                return 1;
            }
        }
        
        public boolean equals(Object o) {
            return hashCode() == o.hashCode();
        }
    }
}
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