結果

問題 No.1221 木 *= 3
ユーザー ks2mks2m
提出日時 2020-09-04 22:51:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 685 ms / 2,000 ms
コード長 1,354 bytes
コンパイル時間 2,128 ms
コンパイル使用メモリ 78,216 KB
実行使用メモリ 88,776 KB
最終ジャッジ日時 2024-05-05 03:24:37
合計ジャッジ時間 12,525 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
36,928 KB
testcase_01 AC 46 ms
37,152 KB
testcase_02 AC 47 ms
36,984 KB
testcase_03 AC 46 ms
36,668 KB
testcase_04 AC 47 ms
37,116 KB
testcase_05 AC 48 ms
37,108 KB
testcase_06 AC 51 ms
37,364 KB
testcase_07 AC 613 ms
84,600 KB
testcase_08 AC 618 ms
84,664 KB
testcase_09 AC 611 ms
88,776 KB
testcase_10 AC 573 ms
83,740 KB
testcase_11 AC 574 ms
83,888 KB
testcase_12 AC 552 ms
70,164 KB
testcase_13 AC 556 ms
67,360 KB
testcase_14 AC 555 ms
68,820 KB
testcase_15 AC 554 ms
68,516 KB
testcase_16 AC 549 ms
68,308 KB
testcase_17 AC 636 ms
67,280 KB
testcase_18 AC 685 ms
68,340 KB
testcase_19 AC 606 ms
66,804 KB
testcase_20 AC 604 ms
72,788 KB
testcase_21 AC 575 ms
62,080 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