Cod sursa(job #1563260)

Utilizator valentin50517Vozian Valentin valentin50517 Data 5 ianuarie 2016 20:08:26
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
typedef struct nod{
    int key;
    nod* next;
}* lnod;
lnod A[16010];
bool B[16010];
int maxi = -1000000000,N,V[16010];
void add(lnod &a,int key){
        lnod b = new nod;
        b->key = key;
        b->next = a;
        a = b;
}
 
int rez(int key){
	if(B[key]) return 0;
    B[key] = 1;
    int rs = 0;
    if(A[key]){
        for(lnod l = A[key];l;l = l->next){      
            rs+=max(0,rez(l->key));
        }
    }
    rs+=V[key];
    if(maxi < rs) maxi = rs;
    return rs;
}
int main(){
    fin >> N;
    for(int i = 1;i<=N;i++) fin >> V[i];
    for(int i = 1;i<N;i++){
            int x,y;
            fin >> x >> y;
            add(A[x],y);
            add(A[y],x);
    }
    rez(1);
    fout << maxi;
        return 0;
}