Cod sursa(job #2844960)

Utilizator mediocrekarmaChirvasa George Matei mediocrekarma Data 6 februarie 2022 16:07:04
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");

constexpr int N_MAX = 16000;
vector<int> adjList[N_MAX + 1];
int cost[N_MAX + 1];
bool fr[N_MAX + 1];
int n, maxim = INT_MIN;

void dfs(const int& node) {
    fr[node] = 1;
    for (const int& x : adjList[node]) {
        if (!fr[x]) {
            dfs(x);
            cost[node] = max(cost[node], cost[x] + cost[node]);
        }
    }
    maxim = max(maxim, cost[node]);
}

int main() {
    ios_base::sync_with_stdio(0);
    fin.tie(0);
    fin >> n;
    for (int i = 1; i <= n; ++i) {
        fin >> cost[i];
    }
    for (int i = 1; i < n; ++i) {
        int x, y;
        fin >> x >> y;
        adjList[x].push_back(y);
        adjList[y].push_back(x);
    }
    dfs(1);
    fout << maxim;
}