Pagini recente » Cod sursa (job #2119459) | Cod sursa (job #2208813) | Cod sursa (job #2975405) | Cod sursa (job #2168587) | Cod sursa (job #1579762)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin ("asmax.in");
ofstream cout ("asmax.out");
vector <int> G[16005];
queue <int> q;
int cost[16005], n, maxx;
bool viz[16005], added;
void read()
{
cin >> n;
for(int i = 1; i <= n; ++i)
{
cin >> cost[i];
}
for(int i = 1; i <= n; ++i)
{
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
}
/*void bfs(int node)
{
///sum of whole tree, not just 1 branch
viz[node] = true;
q.push(node);
while(q.empty() == false)
{
frn = q.front();
if(added == true)
{
sum += cost[frn];
}
if(sum < 0)
{
sum = 0;
}
if(cost[frn] > 0)
{
added = true;
sum += cost[frn];
}
q.pop();
for(auto it: G[frn])
{
if(viz[it] == false and cost[it] > 0)
{
q.push(it);
}
}
}
}*/
void dfs(int node)
{
viz[node] = true;
for(int i = 0; i < (int)G[node].size(); ++i)
{
int next_node = G[node][i];
if(viz[next_node] == false)
{
dfs(next_node);
if(cost[next_node] > 0)
{
cost[node] += cost[next_node];
}
}
}
if(cost[node] > maxx)
{
maxx = cost[node];
}
}
void solve()
{
for(int i = 1; i <= n; ++i)
{
if(G[i].size() == 1)
{
dfs(i);
}
}
}
void print()
{
cout << maxx << " ";
}
int main()
{
read();
solve();
print();
return 0;
}