Cod sursa(job #3280543)

Utilizator _c_lucaCiobotaru Luca _c_luca Data 26 februarie 2025 17:19:16
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <fstream>
#include <vector>

using namespace std;

const int N = 16000;

vector <vector <int>> a(N+1);
int valoare[N+1];
int s_max[N+1];

bool viz[N+1];

void dfs(int x)
{
    viz[x] = true;
    s_max[x]=valoare[x];
    for (auto y: a[x])
    {
        if (!viz[y])
        { 
            dfs(y);
            s_max[x]+=max(0, s_max[y]);
        }
    }
}

int main()
{
    ifstream in("asmax.in");
    ofstream out("asmax.out");
    
    int n;
    in >> n;
    int m=n-1;

    for(int i=1; i<=n; i++){
        in >> valoare[i];
    }

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

    dfs(1);

    int smax=s_max[1];

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

    out<<smax;

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