1099D - Sum in the tree - CodeForces Solution


constructive algorithms trees *1600

Please click on ads to support us..

Python Code:

from __future__ import division, print_function

import threading

threading.stack_size(2**27)
import sys

sys.setrecursionlimit(10**7)


def ain():
    return list(map(int, sin().split()))


def sin():
    return input()


def inin():
    return int(input())


q = 0


def dfs(n, d, v, c, a):
    global q
    if q == -1:
        return
    if a[n - 1] < 0:
        if n not in d:
            return
        x = d[n]
        m = 100000000000
        for i in x:
            if a[i - 1] > -1:
                m = min(m, a[i - 1])
        a[n - 1] = m
    if c > a[n - 1]:
        q = -1
        return
    q += a[n - 1] - c
    v[n] = 1
    if n not in d:
        return
    x = d[n]
    for i in x:
        if i not in v:
            z = a[n - 1]
            _ = dfs(i, d, v, z, a)
    return


def main():
    n = inin()
    a = ain()
    b = ain()
    g = {}
    for i in range(2, n + 1):
        if a[i - 2] in g:
            g[a[i - 2]].append(i)
        else:
            g[a[i - 2]] = [i]
    v = {}
    x = 0
    x = dfs(1, g, v, x, b)
    print(q)


py2 = round(0.5)
if py2:
    from future_builtins import map

    range = xrange
import os
import sys
from io import BytesIO

BUFSIZE = 8192


class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = "x" in file.mode or "w" in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek(
            (self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0]
        )
        return s

    def read(self):
        while self._fill():
            pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill()
            self.newlines = s.count(b"\n") + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()


input = lambda: sys.stdin.readline().rstrip("\r\n")
import sys


class ostream:
    def __lshift__(self, a):
        sys.stdout.write(str(a))
        return self


_ = ostream()
threading.Thread(target=main).start()


Comments

Submit
0 Comments
More Questions

1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even
1269A - Equation
441A - Valera and Antique Items
1702C - Train and Queries
816B - Karen and Coffee
838D - Airplane Arrangements
148B - Escape
847G - University Classes
1110A - Parity
1215B - The Number of Products
604C - Alternative Thinking
1204C - Anna Svyatoslav and Maps
322A - Ciel and Dancing
1689B - Mystic Permutation
1711B - Party
1702D - Not a Cheap String
1714F - Build a Tree and That Is It
1703F - Yet Another Problem About Pairs Satisfying an Inequality