{"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\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eAshish有一棵树,由编号从$$$1$$$到$$$n$$$的$$$n$$$个节点组成,以节点$$$1$$$为根。树中的第$$$i$$$个节点具有成本$$$a_i$$$,并在其中写有二进制数字$$$b_i$$$。他希望最终在第$$$i$$$个节点中写入二进制数字$$$c_i$$$。\u003c/p\u003e\u003cp\u003e为了实现这一目标,他可以执行以下操作任意次数:\u003c/p\u003e\u003cul\u003e\u003cli\u003e从任何节点$$$u$$$的子树中选择任意$$$k$$$个节点,并按照他的意愿重新排列这些节点中的数字,产生成本$$$k \\cdot a_u$$$。在这里,他可以选择$$$k$$$从$$$1$$$到子树$$$u$$$的大小。\u003c/li\u003e\u003c/ul\u003e\u003cp\u003e他希望以这样的方式执行操作,以便最终每个节点都具有与其目标对应的数字。\u003c/p\u003e\u003cp\u003e帮助他找到他需要花费的最小总成本,以便在所有操作之后,每个节点$$$u$$$都有数字$$$c_u$$$写在其中,或确定这是不可能的。\u003c/p\u003e"}},{"title":"输入","value":{"format":"HTML","content":"\u003cp\u003e第一行包含一个整数$$$n$$$,表示树中的节点数。\u003c/p\u003e\u003cp\u003e接下来的$$$n$$$行中,每行包含3个以空格分隔的整数$$$a_i$$$,$$$b_i$$$,$$$c_i$$$,$$$(1 \\leq a_i \\leq 10^9, 0 \\leq b_i, c_i \\leq 1)$$$ —— 第$$$i$$$个节点的成本、其初始数字和目标数字。\u003c/p\u003e\u003cp\u003e接下来的$$$n - 1$$$行中,每行包含两个整数$$$u$$$,$$$v$$$,表示树中节点$$$u$$$和$$$v$$$之间有一条边。\u003c/p\u003e"}},{"title":"输出","value":{"format":"HTML","content":"\u003cp\u003e打印使每个节点达到其目标数字所需的最小总成本,如果不可能则打印$$$-1$$$。\u003c/p\u003e"}},{"title":"示例1","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\u003e5\n1 0 1\n20 1 0\n300 0 1\n4000 0 0\n50000 1 0\n1 2\n2 3\n2 4\n1 5\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e"}},{"title":"示例2","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\u003e5\n10000 0 1\n2000 1 0\n300 0 1\n40 0 0\n1 1 0\n1 2\n2 3\n2 4\n1 5\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e24000\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e"}},{"title":"示例3","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\u003e2\n109 0 1\n205 0 1\n1 2\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e-1\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示例$$$1$$$和$$$2$$$对应的树如下:\u003c/p\u003e\u003ccenter\u003e\u003cimg class\u003d\"tex-graphics\" src\u003d\"CDN_BASE_URL/33247af891469736edd3f0ed7dd429ae?v\u003d1712305247\" style\u003d\"max-width: 100.0%;max-height: 100.0%;\"\u003e\u003c/center\u003e\u003cp\u003e在示例$$$1$$$中,我们可以选择节点$$$1$$$和$$$k \u003d 4$$$,成本为$$$4 \\cdot 1$$$ \u003d $$$4$$$,选择节点$$${1, 2, 3, 5}$$$,重新排列它们的数字,从而使每个节点都得到所需的数字。\u003c/p\u003e\u003cp\u003e在示例$$$2$$$中,我们可以选择节点$$$1$$$和$$$k \u003d 2$$$,成本为$$$10000 \\cdot 2$$$,选择节点$$${1, 5}$$$并交换它们的数字,类似地,选择节点$$$2$$$和$$$k \u003d 2$$$,成本为$$$2000 \\cdot 2$$$,选择节点$$${2, 3}$$$并交换它们的数字,从而使每个节点都得到所需的数字。\u003c/p\u003e\u003cp\u003e在示例$$$3$$$中,不可能获得所需的数字,因为初始时没有具有数字$$$1$$$的节点。\u003c/p\u003e"}}]}