Cod sursa(job #2676203)

Utilizator Ionut2791Voicila Ionut Marius Ionut2791 Data 23 noiembrie 2020 18:05:56
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <limits.h>
#include <algorithm>
using namespace std;

const int N = 16005;

int n, mx = INT_MIN;
vector<int> graf[N];
int cost[N];
bool amfost[N];
bool on[N];


void dfs(int nod) {
    amfost[nod] = true;


    for (int i = 0; i < graf[nod].size(); ++i) {
        int to = graf[nod][i];

        if(amfost[to] == false) {
            dfs(to);
            cost[nod] = max(cost[nod] + cost[to], cost[nod]);
        }
    }
    amfost[nod] = false;
}


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

    int n;
    fin >> n;

    for (int i = 1; i <= n; ++i)
        fin >> cost[i];

    for (int i = 1; i < n; ++i) {
        int a, b;
        fin >> a >> b;
        graf[a].push_back(b);
        graf[b].push_back(a);
    }

    dfs(1);

    for (int i = 1; i <= n; ++i) {
      //  cout << cost[i] << '\n';
        mx = max(cost[i], mx);
    }
    //cout << '\n';
    fout << mx << '\n';
    return 0;
}