Pagini recente » Cod sursa (job #778561) | Cod sursa (job #3276077) | Cod sursa (job #1399256) | Cod sursa (job #699810) | Cod sursa (job #1200162)
#include<iostream>
#include<vector>
#include<fstream>
#define maxn 100005
using namespace std;
ifstream f("asmax.in");
ofstream g("asmax.out");
vector <int> t[maxn];
int i,j,n,m,nod[maxn],valori[maxn],cost;
int max(int a,int b)
{
if(a>b)return a;
else return b;
}
int dfs(int k)
{
nod[k]=1;
int cost=valori[k];
for(int j=0;j<t[k].size();j++)
if(nod[t[k][j]]==0)cost=max(max(dfs(t[k][j]),valori[t[k][j]]+cost),cost);
return cost;
nod[k]=0;
}
int main()
{
f>>n;
for(i=1;i<=n;i++)f>>valori[i];
while(f>>i>>j)
{
t[i].push_back(j);
t[j].push_back(i);
}
int l;
cost=-1001;
for(i=1;i<=n;i++)if(nod[i]==0){l=dfs(i);if(cost<l)cost=l;}
g<<l;
g.close();
return 0;
}