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. Linux bash篇(二 操作环境)

    1.命令执行的顺序 (1).相对/绝对路径 (2).由alias找到的命令 (3).由bash内置的命令 (4).通过$PATH变量找到的第一个命令 2.第一篇讲到的bash在注销后就会无效,如果想保 ...

  2. 8.4 StringBuilder的介绍及用法(String 和StringBuilder区别)

    * StringBuilder:是一个可变的字符串.字符串缓冲区类.** String和StringBuilder的区别:* String的内容是固定的.(方法区的内容)* StringBuilder ...

  3. Java相同id的数据集合,合并数据为一条,并将几个字段内容合并为一个

    Java实现,当然也可以数据库实现; /** * Created by shaozhiqi on 2019/7/31. */ public class TestUnion { @Test public ...

  4. 如何实现Jenkins 编译结果通知到QQ好友及QQ群组<很遗憾 2019年1月1日腾讯停止了webqq机器人的服务支持>

    Jenkins-NotifyQQ NotifyQQ 运行于Docker 本文介绍mac 环境下实现Jenkins编译结果QQ即时通知 Jenkins 安装使用及iOS自动化打包,邮件通知请参考本人博客 ...

  5. Go语言讲解深拷贝与浅拷贝

    我们在开发中会经常的把一个变量复制给另一个变量,那么这个过程,可能是深浅拷贝,那么今天帮大家区分一下这两个拷贝的区别和具体的区别. 一.概念 1.深拷贝(Deep Copy): 拷贝的是数据本身,创造 ...

  6. Sprint 3 : oxford project API 尝试

    本次Sprint我们大家主要在调研和尝试阶段,主要是对photo experience 中的语音接口部分进行相应的调研和分析. 工作进度: 1. 图像界面设计兆阳和敏龙的工作进一步推进,除去之前介绍的 ...

  7. 引用传参与reference_wrapper

    本文是<functional>系列的第3篇. 引用传参 我有一个函数: void modify(int& i) { ++i; } 因为参数类型是int&,所以函数能够修改传 ...

  8. 漫谈LiteOS之开发板-LiteOS移植(基于GD32450i-EVAL)

    1 为什么移植? 嵌入式设备的芯片型号和外设的差异较大,资源有限.而RTOS无法适配集成所有的驱动,因此会先适配部分开发板,然后通过移植使得适配更多的开发板. 可移植性是嵌入式操作系统与普通操作系统的 ...

  9. [YII2] 3步发送邮件,有图有真相!

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABOQAAAIcCAYAAABW0HFSAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjw ...

  10. 新手想掌握Python技能需要众多Python项目练习,适合项目有哪些?

    适合新手练习的Python项目有哪些?简单易上手的Python项目汇总:Web 项目设计:内容聚合器.正则表达式查询工具.网址缩短.便利贴.功能.测验.GUI 项目设计:MP3 播放器.闹铃提醒工具. ...