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. PHP PSR代码规范

    转载: https://www.awaimai.com/916.html PSR是PHP通用性框架小组 (PHP Framework Interop Group) 制定的PHP代码编写格式规范,是PH ...

  2. day 7-5 守护线程

    一. 守护线程 无论是进程还是线程,都遵循:守护进程(线程)会等待主进程(线程)运行完毕后被销毁. 需要强调的是:运行完毕并非终止运行. 1.对主进程来说,运行完毕指的是主进程代码运行完毕. 2.对主 ...

  3. Bootstrap 面板(Panels)

    一.面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .panel-default 即可,如下面的 ...

  4. Nginx安装- CentOS7

    1.确认是否具备安装环境 g++  -v 如果不打印则不具备. 解决办法:联网执行如下命令 yum install gcc yum install gcc-c++ 2.需要材料 pcre-8.37.t ...

  5. python学习笔记(6)--条件分支语句

    if xxxx: coding if xxxx: coding else: coding if xxxx: coding elif xxx: coding …… else: coding 或者一种简洁 ...

  6. 使用IWMS的网站打开显示“未能加载文件或程序集”,解决方案

    首先,会出现这样的问题原因是: 1.应用程序集里面有些事互相引用的,所以 问题有多种情况,第一.这个应用程序集出问题了: 2.它所依赖的那个程序集出问题了: 3.在项目生成的时候,代码里面有逻辑错误: ...

  7. loadrunner 事务、同步点和思考时间

    事务 在LoadRunner里,我们定义事务主要是为了度量服务器的性能.每个事务度量服务器响应指定的Vuser请求所有的时间,这些请求可以是简单任务,也可以是复杂任务. 要度量事务,需要插入Vuser ...

  8. webpack安裝和卸載

    webpack安裝和卸載 安裝: 先裝好node和npm: 安裝package.json:進入到根目錄,運行npm init 新建全局webpack:cd退到全局目錄,運行npm install -g ...

  9. 数据库语法group by

    因为在做pgsql和mysql数据库时group by 有报错,但是在以前做mysql5.6的时候没有问题,虽然知道时违反了sql的语法问题,但是没有搞清楚什么原因,也找了不少资料,查找原因,在盆友的 ...

  10. 解析xml文件 selectSingleNode取不到节点

    今天在做批量生成XML的时候,碰到一个情况 解析xml文件 selectSingleNode一直返回NULL. XML的格式开头有一句这个<CE401Message xmlns="ht ...