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;
}

猜你喜欢

转载自blog.csdn.net/aqa2037299560/article/details/86009151