A. 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
5
1 1 1 1
1 -1 -1 -1 -1
output
1
input
5
1 2 3 1
1 -1 2 -1 -1
output
2
input

 
3
1 2
2 -1 1
output
-1

这道题时说有一颗树,树有N个节点,每个节点有一个值Vi,沿着根节点到I节点求和得到SUMi,前缀和,现在删去vi,且删去了偶数深度节点的sumi,vi>=0,sumi<=1e9.现是否存在一棵
这样的树使得各点vi的和相加最小,不存在就是,存在子节点sumi小于父节点sumi使得子节点vi为负值。当为偶数节点时,他要被做差分,他也要与父节点做差分,如果他有子节点时,
他们几个构成贪心的机制,代价=∑(sum[子节点]-sum[当前节点])+sum[当前节点]-sum[父节点] 可知当子节点大于1的时候sum[当前]越大越好,最大的符合条件的sum[当前]
是 sum【子节点】的最小值,当没有子节点时,显而易见等于父节点的sum【父节点】时代价最小,当只有一个子节点时,代价去sum【子】与sum【父】带价相同,归到第一类,可以写代码了。
#include<bits/stdc++.h>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define speed ios_base::sync_with_stdio(0); // 切不可用scnaf;
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e5+;
int fa[maxn];
ll sum[maxn];
int main()
{
speed
int n;
ll ans=;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>fa[i];
}
for(int i=;i<=n;i++)
{
cin>>sum[i];
if(sum[i]==-) sum[i]=1e9+;
// sum[i]=sum[i]==-1?1e9+1:sum[i];
}
// for(int i=1;i<=n;i++) cout<<sum[i]<<' '<<endl;
// cout<<endl;
for(int i=;i<=n;i++)
{
sum[fa[i]]=Min(sum[fa[i]],sum[i]);
}
// for(int i=1;i<=n;i++) cout<<sum[i]<<' '<<endl;
for(int i=;i<=n;i++)
{
int temp=sum[i]-sum[fa[i]];
if(temp<) return cout<<-<<endl,;
if(sum[i]<=1e9) ans+=temp;
}
cout<<sum[]+ans<<endl;
}

Codeforce 1098-A的更多相关文章

  1. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  2. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  3. 【BZOJ】1098: [POI2007]办公楼biu(补图+bfs+链表)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1098 显然答案是补图连通块..... 想到用并查集...可是连补图的边都已经...n^2了...怎么 ...

  4. LightOj 1098 - A New Function(求1-n所有数的因子和)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1098 题意:给你一个数n (0 ≤ n ≤ 2 * 109),求n以内所有数的因子和, ...

  5. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  6. HDU 1098 Ignatius's puzzle(数学归纳)

    以下引用自http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=8466&messageid=2&deep=1 题意以 ...

  7. HDU 1098 Ignatius's puzzle

    http://acm.hdu.edu.cn/showproblem.php?pid=1098 题意 :输入一个K,让你找一个a,使得f(x)=5*x^13+13*x^5+k*a*x这个f(x)%65等 ...

  8. [BZOJ 1098] [POI2007] 办公楼biu 【链表优化BFS】

    题目链接:BZOJ - 1098 题目分析 只有两个点之间有边的时候它们才能在不同的楼内,那么就是说如果两个点之间没有边它们就一定在同一座楼内. 那么要求的就是求原图的补图的连通块. 然而原图的补图的 ...

  9. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  10. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

随机推荐

  1. JAVA中使用使Math 类操作数据

    转自:https://www.imooc.com/code/2342 侵删! Math 类位于 java.lang 包中,包含用于执行基本数学运算的方法, Math 类的所有方法都是静态方法,所以使用 ...

  2. Array(数组)对象-->splice() 方法

    1.定义和用法 splice() 方法用于添加或删除数组中的元素. 语法: array.splice(index,howmany,item1,.....,itemX) 参数: index:该参数是开始 ...

  3. "Flex弹性布局"组件:<flex-row><flex-col> —— 快应用组件库H-UI

     <import name="flex-row" src="../Common/ui/h-ui/basic/c_flex_row"></im ...

  4. Pod容器共享Volume

    同一个Pod中的多个容器能够共享Pod级别的存储卷Volume.Volume可以被定义为各种类型,多个容器各自进行挂载操作,将一个Volume挂载为容器内部需要的目录,如图 在下面的例子中,在Pod内 ...

  5. web中拖拽排序与java后台交互实现

    一.业务需求 1,在后台的管理界面通过排序功能直接进入排序界面 2,在排序界面能够人工的手动拖动需要排序的标题,完成对应的排序之后提交 3,在app或者是前端就有对应的排序实现了 二.页面展示 将整体 ...

  6. AJ学IOS(33)UI之Quartz2D雪花飘落效果刷帧

    AJ分享,必须精品 效果: 可以加入随机数实现真的飘落效果哦. 代码: -(id)initWithCoder:(NSCoder *)aDecoder { //请注意这里一定要先初始化父类的构造方法 i ...

  7. 选择IT行业的自我心得,希望能帮助到各位!(五)

    相信很多小伙伴,在看完之前的一二三四,也是我一路走来,走走停停,走走停停,有快乐,也有伤悲,毕竟这就是人生嘛,人生不起起伏伏怎么才能体验刺激的快感,也让我从一个小男孩净化成清高浮躁的青少年,在从而让我 ...

  8. 使用 Chrome 插件 Vimium 打造黑客浏览器

    之前一直用 cVim,与 Vimium 功能类似,但是之后不在更新了,故转战到 Vimium. 简介 官网:http://vimium.github.io/ Vimium 是 Google Chrom ...

  9. lua 逻辑运算 and, or, not

    这边并非说lua低级,为了方便区分才这么写的. 高级语言中的逻辑运算符是&&,||,! a&&b : 当a和b都为真, 结果返回为真,当a或者b有一个为假,结果返回为假 ...

  10. api测试用例(编写思路)

    在API的自动化测试维度中,测试维度分为两个维度,一个是单独的对API的验证,客户端发送一个请求后,服务端得到客户端的请求并且响应回复给客户端: 另外一个维度是基于业务场景的测试,基于业务场景的也就是 ...