Pagini recente » Cod sursa (job #2176438) | Cod sursa (job #2230691) | Cod sursa (job #1835113) | Cod sursa (job #1402665) | Cod sursa (job #2218174)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//ifstream fin("omegalul.in");
//ofstream fout("omegalul.out");
ifstream fin("asmax.in");
ofstream fout("asmax.out");
const int N = 16001;
int n;
vector <int> a[N], nod;
bool viz[N];
int d[N];
void cit()
{
fin>>n;
for (int i=1; i<=n; i++)
{
int k;
fin>>k;
nod.push_back(k);
}
for (int i=1; i<=n; i++)
{
int x, y;
fin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
}
void dfs(int x)
{
viz[x] = true;
d[x] += nod[x-1];
for (int i=0; i < a[x].size(); i++)
{
int y = a[x][i];
if (!viz[y])
{
dfs(y);
if (d[y] > 0) d[x] += d[y];
}
}
}
int main()
{
cit();
dfs(1);
int mx = -99999999;
for (int i=1; i<=n; i++)
if (d[i] > mx) mx = d[i];
fout<<mx;
return 0;
}
/*
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//ifstream fin("omegalul.in");
//ofstream fout("omegalul.out");
ifstream fin("easygraph.in");
ofstream fout("easygraph.out");
const long N = 30010, INF = 2e9;
long t, n, m;
vector <long> a[N], nod;
long d[N];
void cit()
{
fin>>n>>m;
for (int i=1; i <= n; i++)
d[i] = -INF;
if (nod.size()) nod.clear();
for (int i=1; i <= n; i++)
a[i].clear();
for (int i=0; i < n; i++)
{
int x;
fin>>x;
nod.push_back(x);
}
for (int i=0; i<m; i++)
{
int x, y;
fin>>x>>y;
a[x].push_back(y);
}
}
int calcul(int x)
{
if (d[x] != -INF) return d[x];
//cout<<"XD ";
d[x] = nod[x-1];
//cout<<"FML "<<x<<" "<<d[x]<<" :( ";
long lol = d[x];
for (int i=0; i < a[x].size(); i++)
{
long y = a[x][i];
//cout<<"??? "<<a[x].size()<<" ??? "<<y<<" ???";
d[x] = max(d[x], lol + calcul(y));
}
//cout<<x<<" "<<nod[x-1]<<" "<<d[x]<<"\n";
return d[x];
}
void dfs(int x, int sum)
{
viz[x] = elem;
sum += nod[x-1];
if (sum > summax) summax = sum;
//cout<<elem<<" "<<x<<" "<<sum<<" "<<summax<<" "<<sumsupreme<<"\n";
int y;
for (int i = 0; i < a[x][t].size(); i++)
{
y = a[x][i][t];
if (viz[y] < elem) dfs(y, sum);
}
}
int main()
{
fin>>t;
for (int j=1; j<=t; j++)
{
//cout<<"\n";
cit();
//for (int i=1; i<=n; i++)
// cout<<"Calcul de "<<i<<" = "<<calcul(i)<<"\n";
for (int i = 1; i <= n; i++)
calcul(i);
long mx = -INF;
for (int i = 1; i <= n; i++)
{
//cout<<i<<" "<<d[i]<<" ";
if (d[i] > mx) mx = d[i];
}
fout<<mx<<"\n";
}
return 0;
}
*/