Cod sursa(job #2402717)

Utilizator soverysourSebastian soverysour Data 10 aprilie 2019 22:44:30
Problema Asmax Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
#include <bits/stdc++.h>
using namespace std;

long int dp[16001];
int cost[16001];

ifstream f("asmax.in");
ofstream g("asmax.out");

unordered_map<int, unordered_map<int, bool>> tree;
unordered_map<int, bool> to_check;

void fill_dp(int n) {
  if ( to_check[n] ) {
    to_check[n] = false;

    for ( auto child : tree[n] ) {
      fill_dp(child.first);
    }

    int t = cost[n];

    for ( auto child : tree[n] ) {
      if ( cost[child.first] > 0 ) {
        t += cost[child.first];
      }
    }

    dp[n] = t;
  }
}

int main()
{
  int n;
  f >> n;

  for ( int i = 1; i <= n; i++ ) {
    f >> cost[i];
  }

  int src, dst;
  for ( int i = 1; i < n; i++ ) {
    f >> src >> dst;
    to_check[src] = true;
    to_check[dst] = true;

    if ( tree.count(src) ) {
      tree[src][dst] = true;
    }
    else {
      tree[src] = unordered_map<int, bool>();
      tree[src][dst] = true;
    }

    if ( tree.count(dst) ) {
      tree[dst][src] = true;
    }
    else {
      tree[dst] = unordered_map<int, bool>();
      tree[dst][src] = true;
    }
  }

  for ( int i = 1; i <= n; i++ ) {
    fill_dp(i);
  }

  int maximu = dp[1];

  for ( int i = 2; i <= n; i++ ) {
    if ( dp[i] > maximu ) {
      maximu = dp[i];
    }
  }

  g << maximu;

  return 0;
}