#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ll long long
#define ull unsigned long long
#define lld long long double
#define read(a) for(int i = 0 ; i < a.size();i++) cin >> a[i];
#define print(a) for(int i = 0 ; i < a.size();i++) cout << a[i] <<" ";
#define rep(i,x,n) for(int i = x ; i <=n; i++)
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int,int>
#define msi multiset<int>
#define si set<int>
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define in insert
#define er erase
#define all(x) x.begin(),x.end()
// Debugger
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
//void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
// Debugger end
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const bool Multiple = 0;
const int MAXN = 1e5 + 10;
vector<int> adj[MAXN];
vector<int> a(MAXN);
vector<int> s(MAXN);
int ans = 0 ;
void dfs(int cur , int par) {
int spar = 0 ;
if (par != -1) spar = s[par];
if (s[cur] == -1) {
int minv = 2e9;
for (auto it : adj[cur]) {
minv = min(minv, s[it]);
}
if (adj[cur].size() == 0) {
minv = spar;
}
s[cur] = minv;
}
for (auto it : adj[cur]) {
dfs(it, cur);
}
a[cur] = s[cur] - spar;
ans = ans + a[cur];
}
void solve() {
int n ;
cin >> n ;
for (int i = 2 ; i <= n ; i++) {
int x ;
cin >> x;
adj[x].pb(i);
}
for (int i = 1 ; i <= n; i++) {
cin >> s[i];
}
dfs(1, -1);
for (int i = 1 ; i <= n ; i++) {
if (a[i] < 0) {
cout << -1 << endl;
return;
}
}
cout << ans << endl;
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1 ;
if (Multiple) cin >> t;
for (int i = 1 ; i <= t ; i++) {
//cout << "Case #" << i << ": ";
solve();
}
return 0 ;
}
1722F - L-shapes | 1196B - Odd Sum Segments |
1325D - Ehab the Xorcist | 552B - Vanya and Books |
1722E - Counting Rectangles | 168A - Wizards and Demonstration |
168B - Wizards and Minimal Spell | 7A - Kalevitch and Chess |
912B - New Year's Eve | 1537C - Challenging Cliffs |
879B - Table Tennis | 1674E - Breaking the Wall |
1282A - Temporarily unavailable | 1366C - Palindromic Paths |
336A - Vasily the Bear and Triangle | 926A - 2-3-numbers |
276D - Little Girl and Maximum XOR | 1253C - Sweets Eating |
1047A - Little C Loves 3 I | 758D - Ability To Convert |
733A - Grasshopper And the String | 216A - Tiling with Hexagons |
1351B - Square | 1225A - Forgetting Things |
1717A - Madoka and Strange Thoughts | 1717B - Madoka and Underground Competitions |
61B - Hard Work | 959B - Mahmoud and Ehab and the message |
802G - Fake News (easy) | 1717C - Madoka and Formal Statement |