From 699c5cffae2eca4bea2547fce201acbe58547dd7 Mon Sep 17 00:00:00 2001 From: oncsr Date: Fri, 4 Jul 2025 09:50:06 +0900 Subject: [PATCH] =?UTF-8?q?[20250705]=20BOJ=20/=20P5=20/=20=EB=8C=80?= =?UTF-8?q?=EA=B8=B0=EC=97=85=20=EC=8A=B9=EB=B2=94=EC=9D=B4=EB=84=A4=20/?= =?UTF-8?q?=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...71\353\262\224\354\235\264\353\204\244.md" | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 "khj20006/202507/05 BOJ P5 \353\214\200\352\270\260\354\227\205 \354\212\271\353\262\224\354\235\264\353\204\244.md" diff --git "a/khj20006/202507/05 BOJ P5 \353\214\200\352\270\260\354\227\205 \354\212\271\353\262\224\354\235\264\353\204\244.md" "b/khj20006/202507/05 BOJ P5 \353\214\200\352\270\260\354\227\205 \354\212\271\353\262\224\354\235\264\353\204\244.md" new file mode 100644 index 00000000..de27910f --- /dev/null +++ "b/khj20006/202507/05 BOJ P5 \353\214\200\352\270\260\354\227\205 \354\212\271\353\262\224\354\235\264\353\204\244.md" @@ -0,0 +1,104 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +public class Main { + + static IOController io; + + // + + static int N; + static int[] A; + static int[][] dp; + static List[] V; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + public static void init() throws Exception { + + N = io.nextInt(); + A = new int[N+1]; + V = new List[N+1]; + for(int i=1;i<=N;i++) V[i] = new ArrayList<>(); + for(int i=2;i<=N;i++) V[io.nextInt()].add(i); + for(int i=1;i<=N;i++) A[i] = io.nextInt(); + dp = new int[N+1][2]; + + } + + static void solve() throws Exception { + + dfs(1); + io.write(Math.max(dp[1][0], dp[1][1]) + "\n"); + + } + + static void dfs(int n) { + for(int i:V[n]) { + dfs(i); + dp[n][0] += Math.max(dp[i][0], dp[i][1]); + } + int max = Integer.MIN_VALUE; + for(int i:V[n]) { + max = Math.max(max, -Math.max(dp[i][0], dp[i][1]) + dp[i][0] + A[i] * A[n]); + } + dp[n][1] = dp[n][0] + max; + } + +} +```