Pagini recente » Cod sursa (job #345498) | Cod sursa (job #1400343) | Cod sursa (job #1911498) | Cod sursa (job #2576126) | Cod sursa (job #800674)
Cod sursa(job #800674)
#include<fstream>
#include<vector>
using namespace std;
const int LIM = 16010; const bool debug = false;
int val[LIM], s[LIM], p[LIM];
vector <int> rel[LIM];
void dfs(int pos, int sum, int caller){
sum+=val[pos];
s[pos]=sum;
p[pos]=caller;
for(int i = 0; i < rel[pos].size(); ++i)
if(rel[pos][i]!=caller) dfs(rel[pos][i], sum, pos);
}
int getValues(int pos){
int sum=s[pos], c;
for(int i=0; i < rel[pos].size(); ++i)
if(rel[pos][i]!=p[pos]){
c = getValues(rel[pos][i]);
if(c-s[pos]>0) sum+=(c-s[pos]);}
return sum;
}
int main(){
ifstream in("asmax.in"); ofstream out("asmax.out");
int n; in>>n;
for(int i=1; i <= n; ++i)
in>>val[i];
int a,b;
for(int i=1; i <= n-1; ++i){
in>>a>>b; rel[a].push_back(b); rel[b].push_back(a);
}
dfs(1, 0, 0);
for(int i = 1; i <= n && debug; ++i)
out<<s[i]<<"\n";
int c, totalsum=0; totalsum+=val[1];
for(int i = 0; i < rel[1].size(); ++i){
c = getValues(rel[1][i]);
if(c-val[1]>0)
totalsum += c-val[1];
}
out<<totalsum<<"\n";
return 0;
}