CF1824E.LuoTianyi and Cartridge

普及/提高-

通过率:0%

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 TT' as follows:

  • First, select pp vertices from TT ( pp is a number chosen by yourself) as the vertex set SS' of TT' .
  • Next, select p1p-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 SS' 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 TT' by the edge with values (cj,dj)(c_j,d_j) ( uu and vv shouldn't be contained in one connected component before in TT' ).

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 SS' , 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 TT' and CC be the minimum of values cic_i in TT' . Let BB be the sum of bib_i in TT' and DD be the sum of values did_i in TT' . Let min(A,C)(B+D)\min(A, C) \cdot (B + D) be the cost of TT' . You need to find the maximum possible cost of TT' .

输入格式

The first line contains one integer nn ( 3n21053\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 ( 1ai21051\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 ( 1bi21051\le b_i\le 2\cdot 10^5 ), where the ii -th integer represents the bib_i value of the ii -th vertex.

Then n1n-1 lines follow, the jj -th of them contains four integers xj,yj,cj,djx_j,y_j,c_j,d_j ( 1xj,yjn,1cj,dj21051\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.

输出格式

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

输入输出样例

  • 输入#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 .

首页