Pagini recente » Cod sursa (job #1473079) | Cod sursa (job #1042693) | Profil ionanghelina | Junior challenge 2020, runda 2 | Cod sursa (job #2998544)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("asmax.in");
ofstream fout("asmax.out");
int val[16001],scor[16001],maxx;
vector<int> a[16001];
bool f[16001];
void dfs(int x)
{
f[x] = true;
scor[x] = val[x];
for (auto y: a[x])
{
if (!f[y])///y este fiu al lui x in arborele DFS
{
dfs(y);
if (scor[y] > 0)
{
scor[x] += scor[y];
}
}
}
}
int main()
{
int n;
fin>>n;
for(int i=1;i<=n;i++)
fin>>val[i];
for(int i=1;i<n;i++)
{
int x,y;
fin>>x>>y;
a[x].push_back(y);
a[y].push_back(x);
}
dfs(1);
maxx=scor[1];
for(int i=2;i<=n;i++)
maxx=max(maxx,scor[i]);
fout<<maxx;
return 0;
}