Pagini recente » Cod sursa (job #1154650) | Cod sursa (job #3283440) | Cod sursa (job #2861164) | Profil VisuianMihai | Cod sursa (job #3280543)
#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;
}