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]的最小值就行了

#include <cstdio>
#include <map>
#include <iostream>
#include<cstring>
#include<bits/stdc++.h>
#define ll long long int
#define M 6
using namespace std;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
int moth[]={,,,,,,,,,,,,};
int dir[][]={, ,, ,-, ,,-};
int dirs[][]={, ,, ,-, ,,-, -,- ,-, ,,- ,,};
const int inf=0x3f3f3f3f;
const ll mod=1e9+;
struct node{
int next;
int to;
};
node edge[];
int head[];
ll v[];
int cnt;
ll ans;
bool ff;
void add(int u,int v){
edge[++cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt;
}
void init(){
cnt=;
ff=;
ans=;
memset(head,,sizeof(head));
}
void dfs(int u,int f){
if(v[u]==-){ //如果是偶数层就统计最小子节点
ll minn=inf;
for(int i=head[u];i!=;i=edge[i].next){
int to=edge[i].to;
if(to==f) continue;
minn=min(minn,v[to]);
}
// cout<<minn<<endl;
v[u]=minn;
}
for(int i=head[u];i!=;i=edge[i].next){
int to=edge[i].to;
if(to==f) continue;
dfs(to,u);
if(v[to]==inf) //如果叶子节点是偶层就等于上面一层
v[to]=v[u];
}
}
void dfs_ans(int u,int f){
if(!ff) return ;
for(int i=head[u];i!=;i=edge[i].next){
int to=edge[i].to;
if(to==f) continue;
ll temp=v[to]-v[u];
if(temp<){ //如果有前缀和有矛盾就出现了错误
ff=;
return ;
}
ans+=temp;
dfs_ans(to,u);
}
}
int main(){
ios::sync_with_stdio(false);
int n;
while(cin>>n){
init();
for(int i=;i<=n;i++){
int t; cin>>t;
add(t,i);
add(i,t);
}
for(int i=;i<=n;i++)
cin>>v[i];
dfs(,);
ans+=v[]; //先加上根结点
dfs_ans(,);
if(!ff) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
return ;
}

codeforces #530 D(Sum in the tree) (树上贪心)的更多相关文章

  1. Codeforces Round #530 (Div. 2) D. Sum in the tree 树上贪心

    D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未 ...

  2. Codeforces1099D.Sum in the tree(贪心)

    题目链接:传送门 思路: 一个节点放的数越大,那么以它为根的子树的节点权值之和就越小. 所以我们要在合法的范围内,使偶数层节点的权值尽可能地大.也就是说,令它的权值是子节点的最小值,这样保证了它的子节 ...

  3. Codeforces Round #530 (Div. 2):D. Sum in the tree (题解)

    D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结 ...

  4. Codeforces Round #530 (Div. 1) 1098A Sum in the tree

    A. Sum in the tree Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root ha ...

  5. Codeforces 1099 D. Sum in the tree-构造最小点权和有根树 贪心+DFS(Codeforces Round #530 (Div. 2))

    D. Sum in the tree time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  7. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )

    树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...

  8. Codeforces 914H Ember and Storm's Tree Game 【DP】*

    Codeforces 914H Ember and Storm's Tree Game 题目链接 ORZ佬 果然出了一套自闭题 这题让你算出第一个人有必胜策略的方案数 然后我们就发现必胜的条件就是树上 ...

  9. [BZOJ 3221][Codechef FEB13] Obserbing the tree树上询问

    [BZOJ 3221]Obserbing the tree树上询问 题目 小N最近在做关于树的题.今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M ...

随机推荐

  1. winform使用相关

    1.回车键触发按钮点击事件——回车登录 设置窗体的AccessButton属性 2.密码框样式设置 设置PasswordChar为想要的密码显示的样式,如*  3.设置窗口居中 设置StartPosi ...

  2. MyEclipse 配置 Tomcat

    安装好Tomcat,MyEclipse 之后,利用这两个工具可以开发部署Web 应用,步骤相对手动部署要简洁的多,这里有一个特别要注意的地方:系统里安装JDK.Tomcat.MyEclipse 的版本 ...

  3. 爱上linux 简单实现移动办公处理环境.

    1. 这周一直在鼓捣linux上面的环境测试. 简单的将 我们的产品部署到了linux上面 详情见前面的 blog 2. 有时候下班了 或者是 在WC (科技园wc排队 说多了都是泪) 或者是眼睛不舒 ...

  4. 给普通用户添加root权限

    >>提君博客原创  http://www.cnblogs.com/tijun/  << 第一步,以root用户查看/etc/sudoers [root@ltt2 hadoop] ...

  5. Day 4-9 subprocess模块

    我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本的模块在python ...

  6. CLOUD物料列表查询的一份跟踪

    SELECT * FROM (SELECT t0.FNUMBER fnumber, t0_L.FNAME fname, t0_L.FSPECIFICATION fspecification, t0.F ...

  7. Python __slots__ 作用

    参考:https://blog.csdn.net/u010733398/article/details/52803643   https://blog.csdn.net/sxingming/artic ...

  8. java学习之—队列

    /** * 队列 * Create by Administrator * 2018/6/11 0011 * 下午 3:27 **/ public class Queue { private int m ...

  9. WPF中如何为ItemsControl添加ScrollViewer并显示ScrollBar

    今天在开发的过程中突然碰到了一个问题,本来的意图是想当ItemsControl中加载的Item达到一定数量时,会出现ScrollViewer并出现垂直的滚动条,但是实际上并不能够达成目标,对于熟手来说 ...

  10. echo显示颜色

    如有转载,不胜荣幸.http://www.cnblogs.com/aaron-agu/ [;;34m hello aaron \[0m”