Pagini recente » Cod sursa (job #2931178) | Cod sursa (job #2847653) | Cod sursa (job #884892) | Cod sursa (job #732688) | Cod sursa (job #846434)
Cod sursa(job #846434)
#include <fstream>
#include <vector>
#include <stack>
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 , comp_conexe , gasit , s1 , s2 , solc , sol , MN;
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);
MN = 1 << 30;
solc = MN * (-1);
sol = MN * (-1);
while(!st.empty())
{
x = st.top();
gasit = 0;
while(!Q[x].empty())
{
if (!viz[Q[x].front()])
{
it = Q[x].begin();
st.push(*it);
viz[*it] = 1;
s1 += cost_nod[*it];
if (MN > s2)
MN = s2;
if (solc < s1 - MN)
solc = s1 - MN;
s2 += cost_nod[*it];
Q[x].erase(Q[x].begin());
gasit = 1;
break;
}
else
Q[x].erase(Q[x].begin());
}
if (!gasit)
st.pop();
}
fout << solc << '\n';
return 0;
}