Cod sursa(job #1256255)

Utilizator gerd13David Gergely gerd13 Data 5 noiembrie 2014 23:21:31
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb
#include <fstream>
#include <iostream>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std ;

const int NMAX = 16005 ;
const int INF = 0x3f3f3f3f ;

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

int N, D[NMAX], A[NMAX], MAX ;
bool B[NMAX] ;
int sol  = -INF ;

vector <int> G[NMAX] ;


inline int max(int a, int b) {
return (a > b ? a : b) ;
}

 void DFS(int x) {


   D[x] = A[x] ;

   for(int i = 0 ; i < G[x].size() ; ++ i)
   if(B[G[x][i]] == 0)
   {

       B[G[x][i]] = 1 ;

        DFS(G[x][i]) ;

        D[x] = max(D[x] + D[G[x][i]], D[x]) ;

        sol = max(sol, D[x]) ;
   }
}

int main()
{
    fin >> N ;
    MAX = -INF ;
    for(int i = 1 ; i <= N ; ++i)
    {
        fin >> A[i] ;
        MAX = max(A[i], MAX) ;
    }

    for(int i = 1 ; i < N ; ++i)
    {
        int x, y ;

        fin >> x >> y ;

        G[x].push_back(y) ;
        G[y].push_back(x) ;
    }

    B[1] = 1 ;

    sol = MAX ;


    DFS(1) ;

    fout << sol << '\n';

    fin.close() ;
    fout.close() ;
    return  0 ;
}