{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\n\u003cscript\u003e\n window.katexOptions \u003d {\n delimiters: [\n {left: \u0027$$$$$$\u0027, right: \u0027$$$$$$\u0027, display: true},\n {left: \u0027$$$\u0027, right: \u0027$$$\u0027, display: false},\n {left: \u0027$$\u0027, right: \u0027$$\u0027, display: true},\n {left: \u0027$\u0027, right: \u0027$\u0027, display: false}\n ]\n };\n\u003c/script\u003e\n","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003e给定一个根树,包含 $$$n$$$ 个顶点。树中的顶点编号从 $$$1$$$ 到 $$$n$$$,根是顶点 $$$1$$$。值 $$$a_i$$$ 写在第 $$$i$$$ 个顶点上。\u003c/p\u003e\u003cp\u003e您可以执行以下操作任意次数(可能为零):选择一个 $$$v$$$ 顶点 \u003cspan class\u003d\"tex-font-style-bf\"\u003e它至少有一个子节点\u003c/span\u003e;将 $$$a_v$$$ 增加 $$$1$$$;并将所有在 $$$v$$$ 的子树中的顶点 $$$a_u$$$ 减少 $$$1$$$(除了 $$$v$$$ 本身)。但是,在每次操作后,所有顶点上的值应为非负。\u003c/p\u003e\u003cp\u003e您的任务是计算使用上述操作后根节点上可能写的最大值。\u003c/p\u003e"}},{"title":"输入","value":{"format":"HTML","content":"\u003cp\u003e第一行包含一个整数 $$$t$$$ ($$$1 \\le t \\le 10^4$$$)\u0026nbsp;— 测试用例的数量。\u003c/p\u003e\u003cp\u003e每个测试用例的第一行包含一个整数 $$$n$$$ ($$$2 \\le n \\le 2 \\cdot 10^5$$$)\u0026nbsp;— 树中顶点的数量。\u003c/p\u003e\u003cp\u003e第二行包含 $$$n$$$ 个整数 $$$a_1, a_2, \\dots, a_n$$$ ($$$0 \\le a_i \\le 10^9$$$)\u0026nbsp;— 写在顶点上的初始值。\u003c/p\u003e\u003cp\u003e第三行包含 $$$n-1$$$ 个整数 $$$p_2, p_3, \\dots, p_n$$$ ($$$1 \\le p_i \\le n$$$),其中 $$$p_i$$$ 是树中第 $$$i$$$ 个顶点的父节点。顶点 $$$1$$$ 是根。\u003c/p\u003e\u003cp\u003e输入的附加约束:所有测试用例中 $$$n$$$ 的总和不超过 $$$2 \\cdot 10^5$$$。\u003c/p\u003e"}},{"title":"输出","value":{"format":"HTML","content":"\u003cp\u003e对于每个测试用例,打印一个整数\u0026nbsp;— 使用上述操作后根节点上可能写的最大值。\u003c/p\u003e"}},{"title":"示例","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e3\n4\n0 1 0 2\n1 1 3\n2\n3 0\n1\n5\n2 5 3 9 6\n3 1 5 2\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e1\n3\n6\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e"}},{"title":"注意","value":{"format":"HTML","content":"\u003cp\u003e在第一个测试用例中,以下操作序列是可能的:\u003c/p\u003e\u003cul\u003e \u003cli\u003e 在 $$$v\u003d3$$$ 上执行操作,然后顶点上的值将变为 $$$[0, 1, 1, 1]$$$; \u003c/li\u003e\u003cli\u003e 在 $$$v\u003d1$$$ 上执行操作,然后顶点上的值将变为 $$$[1, 0, 0, 0]$$$。 \u003c/li\u003e\u003c/ul\u003e"}}]}