1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main_1949_우수마을_고진석 {
5+ static int N ;
6+ static int [] population ;
7+ static List <Integer >[] tree ;
8+ static int [][] dp ;
9+ static boolean [] visited ;
10+
11+ public static void main (String [] args ) throws Exception {
12+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
13+ BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System .out ));
14+ StringTokenizer st ;
15+
16+ N = Integer .parseInt (br .readLine ());
17+
18+ population = new int [N + 1 ];
19+ tree = new ArrayList [N + 1 ];
20+
21+ st = new StringTokenizer (br .readLine ());
22+ for (int i = 1 ; i <= N ; i ++) {
23+ population [i ] = Integer .parseInt (st .nextToken ());
24+ tree [i ] = new ArrayList <>();
25+ }
26+
27+ for (int i = 0 ; i < N - 1 ;i ++) {
28+ st = new StringTokenizer (br .readLine ());
29+ int a = Integer .parseInt (st .nextToken ());
30+ int b = Integer .parseInt (st .nextToken ());
31+ tree [a ].add (b );
32+ tree [b ].add (a );
33+ }
34+
35+ dp = new int [N + 1 ][2 ];
36+ visited = new boolean [N + 1 ];
37+ post (1 );
38+
39+ bw .write (Math .max (dp [1 ][1 ], dp [1 ][0 ]) + "\n " );
40+ bw .flush ();
41+ bw .close ();
42+ br .close ();
43+ }
44+
45+ public static void post (int cur ) {
46+ visited [cur ] = true ;
47+
48+ dp [cur ][1 ] = population [cur ];
49+ dp [cur ][0 ] = 0 ;
50+ for (int to : tree [cur ]) {
51+ if (visited [to ])
52+ continue ;
53+
54+ post (to );
55+
56+ dp [cur ][1 ] += dp [to ][0 ];
57+ dp [cur ][0 ] += Math .max (dp [to ][0 ], dp [to ][1 ]);
58+ }
59+ }
60+
61+ }
0 commit comments