Cod sursa(job #2998536)

Utilizator rapidu36Victor Manz rapidu36 Data 9 martie 2023 18:22:02
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 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 arborele 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;
}