結果

問題 No.417 チューリップバブル
ユーザー tentententen
提出日時 2023-04-05 18:22:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 369 ms / 2,000 ms
コード長 3,392 bytes
コンパイル時間 3,034 ms
コンパイル使用メモリ 93,024 KB
実行使用メモリ 62,436 KB
最終ジャッジ日時 2024-04-10 01:11:11
合計ジャッジ時間 9,288 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
50,200 KB
testcase_01 AC 55 ms
50,372 KB
testcase_02 AC 54 ms
50,404 KB
testcase_03 AC 56 ms
50,424 KB
testcase_04 AC 56 ms
50,256 KB
testcase_05 AC 54 ms
50,420 KB
testcase_06 AC 56 ms
50,428 KB
testcase_07 AC 55 ms
50,312 KB
testcase_08 AC 57 ms
50,384 KB
testcase_09 AC 58 ms
50,468 KB
testcase_10 AC 60 ms
50,504 KB
testcase_11 AC 91 ms
51,952 KB
testcase_12 AC 88 ms
51,356 KB
testcase_13 AC 63 ms
50,180 KB
testcase_14 AC 96 ms
52,288 KB
testcase_15 AC 81 ms
51,572 KB
testcase_16 AC 89 ms
51,504 KB
testcase_17 AC 147 ms
55,080 KB
testcase_18 AC 154 ms
55,196 KB
testcase_19 AC 161 ms
55,896 KB
testcase_20 AC 189 ms
56,272 KB
testcase_21 AC 173 ms
55,972 KB
testcase_22 AC 190 ms
56,312 KB
testcase_23 AC 146 ms
55,220 KB
testcase_24 AC 58 ms
50,284 KB
testcase_25 AC 137 ms
52,892 KB
testcase_26 AC 116 ms
52,980 KB
testcase_27 AC 137 ms
53,240 KB
testcase_28 AC 147 ms
55,320 KB
testcase_29 AC 161 ms
55,400 KB
testcase_30 AC 157 ms
55,256 KB
testcase_31 AC 169 ms
55,472 KB
testcase_32 AC 55 ms
50,472 KB
testcase_33 AC 54 ms
50,252 KB
testcase_34 AC 59 ms
50,540 KB
testcase_35 AC 307 ms
61,244 KB
testcase_36 AC 369 ms
62,436 KB
testcase_37 AC 316 ms
60,352 KB
testcase_38 AC 325 ms
60,524 KB
testcase_39 AC 274 ms
59,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static ArrayList<HashMap<Integer, Integer>> graph = new ArrayList<>();
    static int[] values;
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        int m = sc.nextInt();
        values = new int[n];
        for (int i = 0; i < n; i++) {
            values[i] = sc.nextInt();
            graph.add(new HashMap<>());
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            graph.get(a).put(b, c);
            graph.get(b).put(a, c);
        }
        TreeMap<Integer, Integer> ans = getMap(0, 0, m);
        System.out.println(ans.lastEntry().getValue());
    }
    
    static TreeMap<Integer, Integer> getMap(int idx, int p, int v) {
        if (v < 0) {
            return new TreeMap<>();
        }
        TreeMap<Integer, Integer> ans = new TreeMap<>();
        ans.put(0, values[idx]);
        for (int x : graph.get(idx).keySet()) {
            if (p == x) {
                continue;
            }
            int cost = graph.get(idx).get(x) * 2;
            TreeMap<Integer, Integer> tmp = getMap(x, idx, v - cost);
            Integer key = Integer.MAX_VALUE;
            while ((key = ans.lowerKey(key)) != null) {
                for (int y : tmp.keySet()) {
                    if (y + cost + key > v) {
                        break;
                    }
                    Integer next = y + cost + key;
                    if (ans.floorEntry(next).getValue() >= ans.get(key) + tmp.get(y)) {
                        continue;
                    }
                    int value = ans.get(key) + tmp.get(y);
                    ans.put(next, value);
                    while ((next = ans.higherKey(next)) != null) {
                        if (ans.get(next) > value) {
                            break;
                        }
                        ans.remove(next);
                    }
                }
            }
        }
        return 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