CF1824E.LuoTianyi and Cartridge

NOI/NOI+/CTSC

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

LuoTianyi is watching the anime Made in Abyss. She finds that making a Cartridge is interesting. To describe the process of making a Cartridge more clearly, she abstracts the original problem and gives you the following problem.

You are given a tree TT consisting of nn vertices. Each vertex has values aia_i and bib_i and each edge has values cjc_j and djd_j.

Now you are aim to build a tree T′T' as follows:

  • First, select pp vertices from TT (pp is a number chosen by yourself) as the vertex set S′S' of T′T'.
  • Next, select p−1p-1 edges from TT one by one (you cannot select one edge more than once).
  • May you have chosen the jj-th edge connects vertices xjx_j and yjy_j with values (cj,dj)(c_j,d_j), then you can choose two vertices uu and vv in S′S' that satisfy the edge (xj,yj)(x_j,y_j) is contained in the simple path from uu to vv in TT, and link uu and vv in T′T' by the edge with values (cj,dj)(c_j,d_j) (uu and vv shouldn't be contained in one connected component before in T′T').

A tree with three vertices, min⁡(A,C)=1,B+D=7\min(A,C)=1,B+D=7, the cost is 77.

Selected vertices 22 and 33 as S′S', used the edge (1,2)(1,2) with cj=2c_j = 2 and dj=1d_j = 1 to link this vertices, now min⁡(A,C)=2,B+D=4\min(A,C)=2,B+D=4, the cost is 88.

Let AA be the minimum of values aia_i in T′T' and CC be the minimum of values cic_i in T′T'. Let BB be the sum of bib_i in T′T' and DD be the sum of values did_i in T′T'. Let min⁡(A,C)⋅(B+D)\min(A, C) \cdot (B + D) be the cost of T′T'. You need to find the maximum possible cost of T′T'.

洛天依正在观看动画《来自深渊》。她发现制作“卡带”十分有趣。为了更清晰地描述制作卡带的过程,她对原问题进行了抽象,并向你提出了如下问题。

给定一棵包含 nn 个顶点的树 TT。每个顶点 ii 具有值 aia_i 和 bib_i;每条边 jj 具有值 cjc_j 和 djd_j。

现在你需要构造一棵新树 T′T',具体步骤如下:

  • 首先,从 TT 中任选 pp 个顶点(pp 由你自己决定)作为 T′T' 的顶点集 S′S';
  • 接着,从 TT 中逐条选出 p−1p-1 条边(每条边至多被选一次);
  • 假设你第 jj 次选择的边连接顶点 xjx_j 与 yjy_j,其对应值为 (cj,dj)(c_j, d_j),则你可在 S′S' 中任选两个顶点 uu 和 vv,满足:在 TT 中,边 (xj,yj)(x_j, y_j) 位于 uu 到 vv 的唯一简单路径上;然后在 T′T' 中用一条权值为 (cj,dj)(c_j, d_j) 的边连接 uu 与 vv(注意:在加入该边前,uu 与 vv 在 T′T' 中不能属于同一连通分量)。

一棵含三个顶点的树,min⁡(A,C)=1, B+D=7\min(A,C)=1,\ B+D=7,代价为 77。

选取顶点 22 和 33 作为 S′S',使用边 (1,2)(1,2)(其 cj=2c_j = 2、dj=1d_j = 1)连接这两个顶点,此时 min⁡(A,C)=2, B+D=4\min(A,C)=2,\ B+D=4,代价为 88。

令 AA 表示 T′T' 中所有顶点 aia_i 值的最小值,CC 表示 T′T' 中所有边 cic_i 值的最小值;
令 BB 表示 T′T' 中所有顶点 bib_i 值的总和,DD 表示 T′T' 中所有边 did_i 值的总和;
定义 T′T' 的代价为 min⁡(A,C)⋅(B+D)\min(A, C) \cdot (B + D)。
你需要求出 T′T' 可能取得的最大代价。

输入格式

The first line contains one integer nn (3≤n≤2⋅1053\le n \le 2\cdot 10^5) — the number of vertices in the tree TT.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤2⋅1051\le a_i\le 2\cdot 10^5), where the ii-th integer represents the aia_i value of the ii-th vertex.

The third line contains nn integers b1,b2,…,bnb_1, b_2, \ldots, b_n (1≤bi≤2⋅1051\le b_i\le 2\cdot 10^5), where the ii-th integer represents the bib_i value of the ii-th vertex.

Then n−1n-1 lines follow, the jj-th of them contains four integers xj,yj,cj,djx_j,y_j,c_j,d_j (1≤xj,yj≤n,1≤cj,dj≤2⋅1051\le x_j,y_j\le n,1\le c_j,d_j\le 2\cdot 10^5) representing the edge (xj,yj)(x_j,y_j) and its values cjc_j and djd_j respectively. It's guaranteed that edges form a tree.

第一行包含一个整数 nn(3≤n≤2⋅1053\le n \le 2\cdot 10^5)——树 TT 的顶点数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤2⋅1051\le a_i\le 2\cdot 10^5),其中第 ii 个整数表示第 ii 个顶点的 aia_i 值。

第三行包含 nn 个整数 b1,b2,…,bnb_1, b_2, \ldots, b_n(1≤bi≤2⋅1051\le b_i\le 2\cdot 10^5),其中第 ii 个整数表示第 ii 个顶点的 bib_i 值。

接下来是 n−1n-1 行,第 jj 行包含四个整数 xj,yj,cj,djx_j,y_j,c_j,d_j(1≤xj,yj≤n, 1≤cj,dj≤2⋅1051\le x_j,y_j\le n,\,1\le c_j,d_j\le 2\cdot 10^5),表示边 (xj,yj)(x_j,y_j) 及其对应的值 cjc_j 和 djd_j。保证这些边构成一棵树。

输出格式

Print a single integer — the maximum possible cost of T′T'.

输出一个整数——T′T' 的最大可能代价。

输入输出样例

  • 输入#1

    3
    1 2 2
    1 1 2
    1 2 2 1
    1 3 1 2

    输出#1

    8
  • 输入#2

    5
    2 4 2 1 1
    2 4 4 4 4
    2 5 3 3
    3 5 2 4
    4 2 5 5
    5 1 1 5

    输出#2

    35
  • 输入#3

    6
    5 7 10 7 9 4
    6 9 7 9 8 5
    2 1 5 1
    3 2 2 4
    4 3 6 3
    5 1 7 4
    6 5 6 8

    输出#3

    216
  • 输入#4

    5
    1000 1000 1 1000 1000
    1000 1000 1 1000 1000
    1 2 1 1
    2 3 1000 1000
    3 4 1000 1000
    3 5 1000 1000

    输出#4

    7000000

说明/提示

The tree from the first example is shown in the statement.

The tree from the second example is shown below:

A=1,B=18,C=1,D=17A = 1, B = 18, C = 1, D = 17, so the cost is min⁡(1,1)⋅(18+17)=35\min(1,1) \cdot (18 + 17) = 35.

第一个示例中的树如题面所示。

第二个示例中的树如下所示:

A=1,B=18,C=1,D=17A = 1, B = 18, C = 1, D = 17,因此代价为 min⁡(1,1)⋅(18+17)=35\min(1,1) \cdot (18 + 17) = 35。

输入解题思路,AI测评打分。不知道怎么写?

首页