Pagini recente » Cod sursa (job #3253794) | Cod sursa (job #3315471) | Cod sursa (job #2697227) | Cod sursa (job #2516088) | Cod sursa (job #3303081)
// RULE: There shouldnt be any spaces in the code, except after commas.
// RULE: All variables in main should be declared globally, and can be used before they are defined.
// RULE: All open curly braces should be placed on the same line as the function or control structure they belong to, not on the line after.
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dosare.in"); //strudel
ofstream fout("dosare.out");
long long n,i,a,sum;
long long f[16010];
vector<long long> v[16010];
long long dfs(long long x){
for(long long it:v[x])
f[x]+=dfs(it);
return f[x];
}
bool cmp(long long x,long long y){
return f[x]>f[y];
}
void dfs2(long long x){
long long i=1;
for(long long it:v[x]){
sum+=i*f[it],dfs2(it);
i++;
}
}
int main()
{
fin>>n;
for(i=2;i<=n;i++) fin>>a,v[a].push_back(i);
for(i=1;i<=n;i++) fin>>f[i];
dfs(1);
//sort
for(i=1;i<=n;i++) sort(v[i].begin(),v[i].end(),cmp);
dfs2(1);
fout<<f[1]+sum;
return 0;
}