Cod sursa(job #2998610)

Utilizator cygAlexandruIvanIvan Alexandru cygAlexandruIvan Data 9 martie 2023 19:14:52
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.96 kb
#include <fstream>
#include <vector>

using namespace std;

const int N = 16000;

int val[N+1], scor[N+1];
bool viz[N+1];
vector<int> a[N+1];

void dfs(int x){
    viz[x] = true;
    scor[x] = val[x];

    for(auto y : a[x])
    {
        if(!viz[y])///y este fiu al lui x in DFS
        {
            dfs(y);
            if(scor[y] > 0)
            {
                scor[x] += scor[y];
            }
        }
    }
}

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

    int n;
    in >> n;

    for(int i = 1; i <= n; i++)
        in >> val[i];

    for(int i = 0; i < n - 1; i++)
    {
        int x, y;
        in >> x >> y;
        a[x].push_back(y);
        a[y].push_back(x);
    }

    dfs(1);
    int scor_max = scor[1];

    for(int i = 2; i <= n; i++)
    {
        scor_max = max(scor_max, scor[i]);
    }

    out << scor_max;

    in.close();
    out.close();
    return 0;
}