Cod sursa(job #800674)

Utilizator TheShadowsAlexandru Cristian TheShadows Data 22 octombrie 2012 12:20:51
Problema Asmax Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#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;
}