Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class DWOJ1457 {
- static ArrayList<LinkedList<Integer>> adj = new ArrayList<LinkedList<Integer>>();
- static int[] coins;
- static boolean[] visited;
- static int find(int node, boolean pickedLast, int parent) {
- int solution1 = 0;
- for(int i : adj.get(node)) {
- if(i != parent) {
- solution1 += find(i, false, node);
- }
- }
- if(pickedLast) {
- return solution1;
- }
- int solution2 = coins[node];
- for(int i : adj.get(node)) {
- if(i != parent) {
- solution2 += find(i, true, node);
- }
- }
- return Math.max(solution1, solution2);
- }
- public static void main(String[] args) throws IOException{
- readLine();
- int n = nextInt();
- for(int i = 0; i <= n; i ++) {
- adj.add(new LinkedList<Integer>());
- }
- coins = new int[n + 1];
- for(int i = 0; i < n - 1; i ++) {
- readLine();
- int a = nextInt();
- int b = nextInt();
- adj.get(a).add(b);
- adj.get(b).add(a);
- }
- readLine();
- for(int i = 1; i <= n; i ++) {
- coins[i] = nextInt();
- }
- visited = new boolean[n + 1];
- System.out.println(find(1, false, 0));
- }
- static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
- static StringTokenizer st;
- static void readLine() throws IOException{
- st = new StringTokenizer(in.readLine());
- }
- static int nextInt() {
- return Integer.parseInt(st.nextToken());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment