Skip to content

Commit 699c5cf

Browse files
authored
[20250705] BOJ / P5 / 대기업 승범이네 / 권혁준
1 parent 2deea18 commit 699c5cf

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
public class Main {
51+
52+
static IOController io;
53+
54+
//
55+
56+
static int N;
57+
static int[] A;
58+
static int[][] dp;
59+
static List<Integer>[] V;
60+
61+
public static void main(String[] args) throws Exception {
62+
63+
io = new IOController();
64+
65+
init();
66+
solve();
67+
68+
io.close();
69+
70+
}
71+
72+
public static void init() throws Exception {
73+
74+
N = io.nextInt();
75+
A = new int[N+1];
76+
V = new List[N+1];
77+
for(int i=1;i<=N;i++) V[i] = new ArrayList<>();
78+
for(int i=2;i<=N;i++) V[io.nextInt()].add(i);
79+
for(int i=1;i<=N;i++) A[i] = io.nextInt();
80+
dp = new int[N+1][2];
81+
82+
}
83+
84+
static void solve() throws Exception {
85+
86+
dfs(1);
87+
io.write(Math.max(dp[1][0], dp[1][1]) + "\n");
88+
89+
}
90+
91+
static void dfs(int n) {
92+
for(int i:V[n]) {
93+
dfs(i);
94+
dp[n][0] += Math.max(dp[i][0], dp[i][1]);
95+
}
96+
int max = Integer.MIN_VALUE;
97+
for(int i:V[n]) {
98+
max = Math.max(max, -Math.max(dp[i][0], dp[i][1]) + dp[i][0] + A[i] * A[n]);
99+
}
100+
dp[n][1] = dp[n][0] + max;
101+
}
102+
103+
}
104+
```

0 commit comments

Comments
 (0)