Cod sursa(job #2272311)

Utilizator IulianOleniucIulian Oleniuc IulianOleniuc Data 29 octombrie 2018 22:55:13
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <vector>
#include <fstream>

#define NMAX 16010
using std::vector;

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

int n;
int dp[NMAX];
vector<int> ad[NMAX];

int sol;
bool vis[NMAX];

inline int max(int x, int y) {
    return x > y ? x : y;
}

void dfs(int node) {
    vis[node] = true;
    for (int i = 0; i < ad[node].size(); i++)
        if (!vis[ad[node][i]]) {
            dfs(ad[node][i]);
            dp[node] = max(dp[node], dp[node] + dp[ad[node][i]]);
        }
    sol = max(sol, dp[node]);
}

int main() {
    fin >> n;
    for (int i = 1; i <= n; i++) {
        fin >> dp[i];
        sol += dp[i];
    }

    int x, y;
    for (int i = 1; i < n; i++) {
        fin >> x >> y;
        ad[x].push_back(y);
        ad[y].push_back(x);
    }

    dfs(1);
    fout << sol << '\n';

    fout.close();
    return 0;
}