Cod sursa(job #2135969)

Utilizator darkviper17Dark Viper darkviper17 Data 19 februarie 2018 15:40:17
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

ifstream fin("asmax.in");
ofstream fout("asmax.out");

const int N = 16001;

int n, m, cost[N];

vector <int> a[N];

bool viz[N];

int dfs(int x)
{
  viz[x] = true;
   
  int y, i;
   
  for(i = 0; i < a[x].size(); i++)
  {
    y = a[x][i];
     
    if(!viz[y]) 
    {
      dfs(y);
      
      if(cost[y] > 0) cost[x] += cost[y];
    }
    
  }
}

void read()
{
  fin >> n;
  
  int i, x, y;
  
  for(i = 1; i <= n; i++)
    fin >> cost[i];
  
  m = n - 1;
  
  for(i = 1; i <= m; i++)
  {
    fin >> x >> y;
    a[x].push_back(y);
    
    a[y].push_back(x);
  }
}

int main() {
  
  read();
  
  int i, total = -999999999;
  
  dfs(1);
  
  for(i = 1; i <= n; i++)
  {
     total = max(total, cost[i]);
  }
  
  fout <<'\n' << total;
  
  fin.close();
  
  return 0;
}