Pagini recente » Istoria paginii utilizator/laura666 | Rating Lechintan Tudor Cristian (LechintanTudor) | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #2453989)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream cin("asmax.in");
ofstream cout("asmax.out");
vector < int > v[16005];
stack < pair < int, int > > st;
int dp[16005],val[16005],viz[16005],n;
void read()
{
int i,j,a,b;
cin>>n;
for(i=1; i<=n; i++)
cin>>val[i];
for(i=1; i<n; i++)
{
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
for(i=1; i<=n; i++)
{
dp[i]=val[i];
}
}
void solve()
{
int i,j,a,b,parent;
bool del;
st.push({1,0});
viz[1]=1;
do
{
a=st.top().first;
parent=st.top().second;
del=true;
for(i=0; i<v[a].size(); i++)
{
b=v[a][i];
if(viz[b]==0)
{
//cout<<b<<" ";
del=false;
st.push({b,a});
viz[b]=1;
}
}
if(del==true)
{
if(dp[a]>0)
dp[parent]+=dp[a];
st.pop();
}
}
while(st.size()>1);
int sol=-99999999;
for(i=1; i<=n; i++)
{
sol=max(sol,dp[i]);
//cout<<dp[i]<<" ";
}
cout<<sol;
}
int main()
{
read();
solve();
return 0;
}
/*
5
-1 1 1 1 1
4 1
1 3
1 2
4 5
*/