結果

問題 No.417 チューリップバブル
ユーザー tentententen
提出日時 2023-01-27 19:20:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 336 ms / 2,000 ms
コード長 3,386 bytes
コンパイル時間 2,790 ms
コンパイル使用メモリ 102,812 KB
実行使用メモリ 65,612 KB
最終ジャッジ日時 2023-09-10 13:13:49
合計ジャッジ時間 9,818 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,360 KB
testcase_01 AC 44 ms
47,484 KB
testcase_02 AC 45 ms
49,636 KB
testcase_03 AC 45 ms
49,312 KB
testcase_04 AC 44 ms
49,636 KB
testcase_05 AC 44 ms
49,648 KB
testcase_06 AC 45 ms
49,356 KB
testcase_07 AC 45 ms
49,440 KB
testcase_08 AC 49 ms
49,380 KB
testcase_09 AC 49 ms
49,380 KB
testcase_10 AC 54 ms
49,532 KB
testcase_11 AC 76 ms
51,840 KB
testcase_12 AC 69 ms
51,964 KB
testcase_13 AC 63 ms
50,968 KB
testcase_14 AC 87 ms
52,028 KB
testcase_15 AC 82 ms
52,532 KB
testcase_16 AC 84 ms
52,076 KB
testcase_17 AC 137 ms
55,860 KB
testcase_18 AC 134 ms
55,492 KB
testcase_19 AC 143 ms
55,680 KB
testcase_20 AC 170 ms
55,628 KB
testcase_21 AC 162 ms
55,480 KB
testcase_22 AC 175 ms
56,052 KB
testcase_23 AC 137 ms
55,944 KB
testcase_24 AC 51 ms
49,568 KB
testcase_25 AC 120 ms
53,304 KB
testcase_26 AC 103 ms
52,872 KB
testcase_27 AC 119 ms
53,460 KB
testcase_28 AC 131 ms
53,404 KB
testcase_29 AC 152 ms
55,352 KB
testcase_30 AC 138 ms
55,532 KB
testcase_31 AC 162 ms
55,776 KB
testcase_32 AC 45 ms
49,488 KB
testcase_33 AC 45 ms
49,564 KB
testcase_34 AC 52 ms
49,480 KB
testcase_35 AC 302 ms
63,716 KB
testcase_36 AC 336 ms
65,612 KB
testcase_37 AC 307 ms
63,404 KB
testcase_38 AC 296 ms
62,560 KB
testcase_39 AC 229 ms
60,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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