Pagini recente » Cod sursa (job #1196205) | Cod sursa (job #3355039) | Cod sursa (job #3311688) | Cod sursa (job #2060710) | Cod sursa (job #3303080)
// 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");
int n,i,a,sum;
int f[16010];
vector<int> v[16010];
int dfs(int x){
for(int it:v[x])
f[x]+=dfs(it);
return f[x];
}
bool cmp(int x,int y){
return f[x]>f[y];
}
void dfs2(int x){
int i=1;
for(int 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;
}