CodeForces - 1099D Sum in the tree 【树上贪心】

D. Sum in the tree

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 vvto 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

题意要求minsum(a[i]),同时题目给了一部分s[i]的值,若确定了所有s[i]的值,那么求a[i]是很容易的。

所以我们先处理s[i]

对于树中任意一个节点,若s[i]是未知的,那么需要对其进行求解。

因为s[i]表示根节点到i节点的所有权值之和,并且对于任意一个节点a[i]的值,a[i]=s[i]-s[u],其中u为i的父亲节点

那么对于这个未知的s[i],我们只需使其值尽可能的大,那么它子节点的权值就会尽可能的小。

先跑一遍dfs确定s[i]的值,再跑一遍确定a[i]的值即可。

#include "bits/stdc++.h"
using namespace std;
const long long inf = 8e18;
const int mod = 998244353;
struct edge
{
    int v,nxt;
}g[200004];
int head[200004];
int cnt;
int n,m;
long long s[200004];
void addedge(int u,int v)
{
    g[cnt].v=v;
    g[cnt].nxt=head[u];
    head[u]=cnt;
    ++cnt;
}
long long a[200004];
int ok;
void dfs(int u)
{
    if(s[u]==-1)
    {
        long long mini=inf;
        for (int i = head[u]; i !=-1 ; i=g[i].nxt) {
            int v=g[i].v;
            dfs(v);
            mini=min(mini,s[v]);
        }
        s[u]=mini;
    }
    else
    {
        for (int i = head[u]; i !=-1 ; i=g[i].nxt) {
            int v=g[i].v;
            dfs(v);
        }
    }
}
void dfs2(int u,int w)
{
    if(s[u]==inf)s[u]=w;
    a[u]=s[u]-w;
    if(a[u]<0){
        ok=0;
        return ;
    }
    for (int i = head[u]; i !=-1 ; i=g[i].nxt) {
        int v=g[i].v;
        dfs2(v,s[u]);
    }
}
int main()
{
    memset(head,-1, sizeof(head));
    cnt=0;
    int n;
    scanf("%d",&n);
    int v;
    for (int i = 2; i <= n; ++i) {
        scanf("%d",&v);
        addedge(v,i);
    }
    ok=1;
    for (int i = 1; i <= n; ++i) {
        scanf("%lld",&s[i]);
    }
    dfs(1);
    dfs2(1,0);
    if(ok)
    {
        long long ans=0;
        for (int i = 1; i <= n; ++i) {
            ans+=a[i];
        }
        printf("%lld\n",ans);
    }
    else puts("-1");
    return 0;
    //printf("%d\n",ans);
}

猜你喜欢

转载自blog.csdn.net/qq_42671946/article/details/86251689