Cod sursa(job #2420770)

Utilizator stefanut999Paul Colta stefanut999 Data 13 mai 2019 11:28:02
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <map>
#include <cmath>
#include <vector>
#include <algorithm>
#define nmax 16001
#define max(a,b) a > b ? a : b
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");

int n, maxim = -1e9;
vector <int> a[nmax];
bool vizitat[nmax];
int s[nmax];

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

void dfs(int x)
{
  vizitat[x] = 1;
  for(auto y : a[x])
    {
      if(!vizitat[y])
          {
           dfs(y);
           if(s[y] > 0)
            s[x] += s[y];
          }
    }
}

int main()
{ citire();
  dfs(1);
  int i;
  for(i = 1; i <= n; ++i)
    maxim = max(maxim, s[i]);
  fout << maxim;
  return 0;
}