結果

問題 No.1221 木 *= 3
ユーザー tentententen
提出日時 2023-04-11 15:48:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 742 ms / 2,000 ms
コード長 2,551 bytes
コンパイル時間 4,003 ms
コンパイル使用メモリ 93,792 KB
実行使用メモリ 83,844 KB
最終ジャッジ日時 2024-04-16 13:38:26
合計ジャッジ時間 16,170 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
37,316 KB
testcase_01 AC 52 ms
37,124 KB
testcase_02 AC 54 ms
37,292 KB
testcase_03 AC 53 ms
37,276 KB
testcase_04 AC 52 ms
37,288 KB
testcase_05 AC 53 ms
37,312 KB
testcase_06 AC 51 ms
37,172 KB
testcase_07 AC 729 ms
83,844 KB
testcase_08 AC 707 ms
74,140 KB
testcase_09 AC 742 ms
80,972 KB
testcase_10 AC 677 ms
76,104 KB
testcase_11 AC 629 ms
81,264 KB
testcase_12 AC 655 ms
63,500 KB
testcase_13 AC 642 ms
62,508 KB
testcase_14 AC 684 ms
64,356 KB
testcase_15 AC 625 ms
64,568 KB
testcase_16 AC 669 ms
63,164 KB
testcase_17 AC 715 ms
62,896 KB
testcase_18 AC 676 ms
61,480 KB
testcase_19 AC 681 ms
61,864 KB
testcase_20 AC 696 ms
61,904 KB
testcase_21 AC 680 ms
60,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    static long[] losts;
    static long[] exists;
    static long[] dp1;
    static long[] dp2;
    static ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int n = sc.nextInt();
        losts = new long[n];
        for (int i = 0; i < n; i++) {
            losts[i] = sc.nextInt();
            graph.add(new ArrayList<>());
        }
        exists = new long[n];
        for (int i = 0; i < n; i++) {
            exists[i] = sc.nextInt();
        }
        for (int i = 0; i < n - 1; i++) {
            int a = sc.nextInt() - 1;
            int b = sc.nextInt() - 1;
            graph.get(a).add(b);
            graph.get(b).add(a);
        }
        dp1 = new long[n];
        dp2 = new long[n];
        calc(0, 0);
        System.out.println(Math.max(dp1[0], dp2[0]));
    }
    
    static void calc(int idx, int p) {
        dp1[idx] = losts[idx];
        for (int x : graph.get(idx)) {
            if (x == p) {
                continue;
            }
            calc(x, idx);
            dp1[idx] += Math.max(dp1[x], dp2[x]);
            dp2[idx] += Math.max(dp1[x], dp2[x] + exists[idx] + exists[x]);
        }
    }
}
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