Pagini recente » Cod sursa (job #672952) | Cod sursa (job #2626089) | Cod sursa (job #1782037) | Cod sursa (job #1475888) | Cod sursa (job #846663)
Cod sursa(job #846663)
#include <fstream>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
using namespace std;
const char iname[] = "asmax.in";
const char oname[] = "asmax.out";
ifstream fin(iname);
ofstream fout(oname);
int N , M , i , j , x , y , gasit , solc;
int vec[16004], dad[16004];
int cost_nod[ 16004 ];
bool viz[ 16004 ];
vector < int > Q[ 16004 ];
stack < int > st;
int main()
{
fin >> N;
for (i = 1; i <= N; ++i)
fin >> cost_nod[i];
for (i = 1; i <= N-1; ++i)
fin >> x >> y , Q[x].push_back(y) , Q[y].push_back(x);
vector < int > :: iterator it;
viz[1] = 1;
st.push(1);
for (i = 1; i <= N; ++i) vec[i] = cost_nod[i];
while(!st.empty())
{
x = st.top();
gasit = 0;
while(!Q[x].empty())
{
if (!viz[Q[x].front()])
{
it = Q[x].begin();
dad[*it] = x;
st.push(*it);
viz[*it] = 1;
Q[x].erase(Q[x].begin());
gasit = 1;
break;
}
else
Q[x].erase(Q[x].begin());
}
if (!gasit)
{
st.pop();
if (vec[x] > 0)
vec[dad[x]] += vec[x];
}
}
solc = INF * (-1);
for (i = 1; i <= N; ++i) if (solc < vec[i]) solc = vec[i];
fout << solc << '\n';
return 0;
}