标签:The for out class \n sum sync const clu
贪心
#include<bits/stdc++.h> const int maxn = 100100; int n; int fa[maxn]; int s[maxn],s2[maxn]; int main(){ std::ios::sync_with_stdio(false),std::cin.tie(0); std::cin >> n; for(int i=2;i<=n;++i)std::cin >> fa[i]; for(int i=1;i<=n;++i)std::cin >> s[i],s2[i]=s[i],s[i]<0?s[i]=2e9:0; for(int i=n;i!=1;--i)if(s[fa[i]]>s[i])s[fa[i]]=s[i]; long long sum=0; for(int i=1;i<=n;++i)if(s2[i]>=0&&s2[i]!=s[i])return std::cout << -1 << ‘\n‘,0; else if(s[i]<=1e9)sum+=s[i]-s[fa[i]]; std::cout << sum << ‘\n‘; }
标签:The for out class \n sum sync const clu
原文地址:https://www.cnblogs.com/skip1978/p/10337964.html