結果

問題 No.1221 木 *= 3
ユーザー ks2mks2m
提出日時 2020-09-04 22:51:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 833 ms / 2,000 ms
コード長 1,354 bytes
コンパイル時間 4,421 ms
コンパイル使用メモリ 75,048 KB
実行使用メモリ 95,844 KB
最終ジャッジ日時 2023-08-17 20:30:23
合計ジャッジ時間 17,719 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,204 KB
testcase_01 AC 45 ms
47,240 KB
testcase_02 AC 45 ms
49,116 KB
testcase_03 AC 45 ms
49,692 KB
testcase_04 AC 45 ms
47,340 KB
testcase_05 AC 45 ms
49,500 KB
testcase_06 AC 46 ms
49,504 KB
testcase_07 AC 759 ms
91,740 KB
testcase_08 AC 769 ms
95,844 KB
testcase_09 AC 730 ms
91,172 KB
testcase_10 AC 710 ms
89,876 KB
testcase_11 AC 693 ms
89,588 KB
testcase_12 AC 743 ms
78,176 KB
testcase_13 AC 780 ms
77,472 KB
testcase_14 AC 711 ms
75,048 KB
testcase_15 AC 725 ms
74,296 KB
testcase_16 AC 720 ms
74,528 KB
testcase_17 AC 833 ms
73,396 KB
testcase_18 AC 789 ms
75,700 KB
testcase_19 AC 780 ms
73,716 KB
testcase_20 AC 753 ms
74,184 KB
testcase_21 AC 754 ms
75,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	static int n;
	static int[] a, b;
	static List<List<Integer>> list;
	static long[] dp0, dp1;

	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		n = Integer.parseInt(br.readLine());

		String[] sa = br.readLine().split(" ");
		a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = Integer.parseInt(sa[i]);
		}

		sa = br.readLine().split(" ");
		b = new int[n];
		for (int i = 0; i < n; i++) {
			b[i] = Integer.parseInt(sa[i]);
		}

		list = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < n - 1; i++) {
			sa = br.readLine().split(" ");
			int u = Integer.parseInt(sa[0]) - 1;
			int v = Integer.parseInt(sa[1]) - 1;
			list.get(u).add(v);
			list.get(v).add(u);
		}
		br.close();

		dp0 = new long[n];
		dp1 = new long[n];
		dfs(0, -1);
		System.out.println(Math.max(dp0[0], dp1[0]));
	}

	static void dfs(int cur, int p) {
		for (int next : list.get(cur)) {
			if (next == p) {
				continue;
			}
			dfs(next, cur);
			dp0[cur] += Math.max(dp0[next], dp1[next]);
			dp1[cur] += Math.max(dp0[next], dp1[next] + b[cur] + b[next]);
		}
		dp0[cur] += a[cur];
	}
}
0