Cod sursa(job #1920374)

Utilizator RaresEGaySopterean Adrian RaresEGay Data 9 martie 2017 23:50:27
Problema Asmax Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.87 kb
#include <fstream>
#include <iostream>
#include <vector>

#define maxn 100005
#define INF 0x3f3f3f3f

using namespace std;

ifstream f("asmax.in");
ofstream g("asmax.out");

vector <int> v[maxn];

int n, m, maxim = -INF;
int visited[maxn], cost[maxn];

void DFS(int current){
    visited[current] = 1;
    for(int i = 0; i < v[current].size(); ++i){
        int nod = v[current][i];
        if(!visited[nod]){
            DFS(nod);
            cost[current] = max(cost[current], cost[current] + cost[nod]);
        }
    }
}

int main(){
    f >> n;
    for(int i = 1; i <= n; ++i) f >> cost[i];
    for(int i = 1; i <= n-1; ++i){
        int x, y;
        f >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    DFS(1);
    for(int i = 1; i <= n; ++i){
        maxim = max(maxim, cost[i]);
    }
    g << maxim << '\n';

}