Cod sursa(job #1584327)

Utilizator PTAdrian64Pop-Tifrea Adrian PTAdrian64 Data 29 ianuarie 2016 22:22:40
Problema Asmax Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.65 kb
#include <cstdio>
#define nmax 16002
#include <queue>
#include <algorithm>

using namespace std;

int N,d[nmax],v[nmax],rez=0,s[nmax];
bool t[nmax][nmax];
queue <int> Q;

void dfs(int x){
  int i;
  for(i =x + 1;i <= N;++i)
    if(t[i][x] == 1){
      dfs(i);
      s[x] = max(s[x],s[x]+s[i]);
    }
  rez = max(rez,s[x]);
}

int main(){
  
  freopen("asmax.in","r",stdin);
  freopen("asmax.out","w",stdout);

  scanf("%d ",&N);
  int i,x,y;
  for(i = 1;i <= N;++i){
    scanf("%d ",&v[i]);
    s[i] = v[i];
  }
  
  for(i=1;i < N;++i){
    scanf("%d %d ",&x,&y);
    t[x][y] = t[y][x] = 1;
    d[x]++;d[y]++;
  }
 
  dfs(1);
  printf("%d\n",rez);

  return 0;
}