Cod sursa(job #2851521)

Utilizator Dragono63Stanciu Rares Stefan Dragono63 Data 18 februarie 2022 19:20:35
Problema Asmax Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <bits/stdc++.h>
#define NMAX 16005
#define INF (NMAX * 1005)

using namespace std;

/*******************************/
// INPUT / OUTPUT
ifstream f("asmax.in");
ofstream g("asmax.out");
/*******************************/
/// GLOBAL DECLARATIONS

int N, ans;
int sum[NMAX], val[NMAX];
vector <int> adj[NMAX];
/*******************************/
/// FUNCTIONS

void ReadInput();
void Solution();
void Output();
/*******************************/
///-------------------------------------
inline void ReadInput()
{
    f >> N;

    for (int i = 1 ; i <= N ; ++ i)
    {
        f >> val[i];
    }

    int a, b;
    for (int i = 1 ; i <= N - 1 ; ++ i)
    {
        f >> a >> b;

        adj[a].push_back(b);
        adj[b].push_back(a);
    }
}
///-------------------------------------
void DFS(int node, int par)
{
    sum[node] = val[node];

    for (auto u: adj[node])
    {
        if (u != par)
        {
            DFS(u, node);
            sum[node] = max(sum[node], sum[node] + sum[u]);
        }
    }
    ans = max(ans, sum[node]);
}
///-------------------------------------
inline void Solution()
{
    ans = -INF;
    DFS(1, 0);
}
///-------------------------------------
inline void Output()
{
    g << ans;
}
///-------------------------------------
int main()
{
    ReadInput();
    Solution();
    Output();
    return 0;
}