Cod sursa(job #3229363)

Utilizator rapidu36Victor Manz rapidu36 Data 15 mai 2024 16:54:54
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1 kb
#include <fstream>
#include <vector>

using namespace std;

const int N = 16000;
const int INF= 1000;

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

void dfs(int x)
{
    viz[x] = true;
    scor[x] = v[x];
    for (auto y: a[x])
    {
        if (!viz[y])///y este fiu al lui x in arborele indus de 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 >> v[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 = -INF;
    for (int i = 1; i <= n; i++)
    {
        scor_max = max(scor_max, scor[i]);
    }
    out << scor_max << "\n";
    in.close();
    out.close();
    return 0;
}