1. 程式人生 > >Codeforces Round #530 (Div. 2) D. Sum in the tree

Codeforces Round #530 (Div. 2) D. Sum in the tree

Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has computed svsv: the sum of all values written on the vertices on the path from vertex vv to the root, as well as hvhv — the depth of vertex vv, which denotes the number of vertices on the path from vertex vv to the root. Clearly, s1=a1s1=a1 and h1=1h1=1.

Then Mitya erased all numbers avav, and by accident he also erased all values svsv for vertices with even depth (vertices with even hvhv). Your task is to restore the values avav for every vertex, or determine that Mitya made a mistake. In case there are multiple ways to restore the values, you're required to find one which minimizes the total sum of values avav for all vertices in the tree.

Input

The first line contains one integer nn — the number of vertices in the tree (2≤n≤1052≤n≤105). The following line contains integers p2p2, p3p3, ... pnpn, where pipi stands for the parent of vertex with index ii in the tree (1≤pi<i1≤pi<i). The last line contains integer values s1s1, s2s2, ..., snsn (−1≤sv≤109−1≤sv≤109), where erased values are replaced by −1−1.

Output

Output one integer — the minimum total sum of all values avav in the original tree, or −1−1 if such tree does not exist.

Examples

input

Copy

5
1 1 1 1
1 -1 -1 -1 -1

output

Copy

1

input

Copy

5
1 2 3 1
1 -1 2 -1 -1

output

Copy

2

input

Copy

3
1 2
2 -1 1

output

Copy

-1

題目意思:

給出一棵樹,以及每個節點到根節點的路徑上經過的所有點的權值之和(包括這個節點在內),其中題目把所有深度為偶數的節點的資訊全部擦除了,也就是用-1表示,讓你求最終所有點權之和(要求最小)。

思路:

奇數層的資訊都是已知的,我們只需要求出偶數層的就行了,只需要使得偶數層節點的s[i]等於其所有子節點s[i]的最小值就行了(如果再大一點就不合法了,因為點權沒有負數,再小一點就不能得出總權值的最小值了)。最後還需要判斷一下所有葉子節點的情況,如果葉子節點是偶數層,那麼其s[i]就繼承其父親節點的值(相當於這個節點的權值為0)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

typedef long long ll;
const int maxn = 1e5 + 100;

int top, n;
int head[maxn], depth[maxn];
ll val[maxn];

struct node {
	int v, next;
}edge[maxn * 2];

inline void add(int u, int v) {        //鏈式前向星存樹
	edge[top].v = v;
	edge[top].next = head[u];
	head[u] = top++;
}

inline void Init() {              //初始化
	top = 0;   
	memset(head, -1, sizeof(head));
}

ll ans;
bool flag;

void dfs(int u, int father) {
	if(val[u] == -1) {             //如果該節點為偶數層,找到其子節點的最小值
		ll mins = 0x3f3f3f3f;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(v != father) {
				mins = min(mins, val[v]);       //求最小值
			}
		}
		val[u] = mins;
	}
	for(int i = head[u]; i != -1; i = edge[i].next) {
		int v = edge[i].v;
		if(v != father) {
			dfs(v, u);             //然後繼續遞迴
			if(val[v] == 0x3f3f3f3f) {    //順便處理一下葉子節點為偶數層的情況
				val[v] = val[u];
			}
		}
	}
}

void dfs_ans(int u, int father) {    //遞迴一遍求答案
	if(!flag) return ;
	for(int i = head[u]; i != -1; i = edge[i].next) {
		int v = edge[i].v;
		if(v != father) {
			ll temp = val[v] - val[u];
			if(temp < 0) {           //答案不合法
				flag = false;
				return ;
			}
			ans += temp;         //答案加上
			dfs_ans(v, u);
		}
	}
}

int main()
{
	//freopen("in.txt", "r", stdin);
	cin >> n;
	Init();
	int x;
	for(int i = 1; i <= n - 1; ++ i) {
		scanf("%d", &x);
		add(i + 1, x);
		add(x, i + 1);
	}
	for(int i = 1; i <= n; ++ i) {
		scanf("%I64d", &val[i]);
		//scanf("%lld", &val[i]);
	}
	ans = val[1];       //先把1號節點的權值加上
	flag = true;
	dfs(1, 0);        //補充位置節點資訊
	dfs_ans(1, 0);        //計算答案
	if(!flag)              //先檢視是否合法
		cout << -1 << endl;
	else
		cout << ans << endl;
	return 0;
}