Cod sursa(job #1889853)

Utilizator razvan242Zoltan Razvan-Daniel razvan242 Data 22 februarie 2017 21:52:57
Problema Asmax Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.99 kb
#include <fstream>
#include <vector>

using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using graph = vector<vi>;

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

vi d, cost;
vb viz;
graph v;
int n, ans = -2e9;

inline void read() {
    fin >> n;
    d = vi(n + 1, -2e9);
    cost = vi(n + 1);
    v = graph(n + 1);
    viz = vb(n + 1);
    for (int i = 1; i <= n; ++i)
        fin >> cost[i];
    int x, y;
    for (int i = 1; i < n; ++i) {
        fin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
}

void dfs(int node) {
    viz[node] = 1;
    d[node] = cost[node];
    for (const int& y: v[node]) {
        if (!viz[y]) {
            dfs(y);
            d[node] = max(d[node], d[node] + cost[y]);
        }
    }
    ans = max(ans, d[node]);
}

inline void solve() {
    dfs(1);
    fout << ans << '\n';
}

int main()
{
    read();
    solve();
    fin.close();
    fout.close();
    return 0;
}