Pagini recente » Cod sursa (job #1161329) | Cod sursa (job #969593) | Cod sursa (job #1788142) | Istoria paginii runda/21312/clasament | Cod sursa (job #1848661)
#include <fstream>
#include <vector>
#define nmax 16001
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int n,v[nmax],cost[nmax],maxx=-1*nmax;
bool viz[nmax];
vector < int > leg[nmax];
inline void read()
{
int i,a,b;
fin>>n;
for(i=1; i<=n; i++)
{
fin>>v[i];
cost[i]=v[i];
}
for(i=1; i<n; i++)
{
fin>>a>>b;
leg[a].push_back(b);
leg[b].push_back(a);
}
fin.close();
}
void DFS(int x)
{
int i;
viz[x]=true;
for(i=0; i<leg[x].size(); i++)
{
if(viz[leg[x][i]]==false)
{
DFS(leg[x][i]);
if(cost[leg[x][i]]>0)
cost[x]+=cost[leg[x][i]];
}
if(cost[x]>maxx)
maxx=cost[x];
}
}
int main()
{
read();
DFS(1);
fout<<maxx;
fout.close();
return 0;
}