#P9481. [NOI2023] 贸易

[NOI2023] 贸易

Problem Description

In recent years, Country A’s commerce has grown rapidly, but the construction of domestic roads has failed to keep up, which has clearly become a restriction on people’s trade activities. The administrators have put a lot of effort into this.

Specifically, Country A has a total of 2n−12^n-1 cities, where city 11 is the capital. For every non-capital city ii, there is a directed road starting from city ii and going to city ⌊i2⌋\lfloor \frac{i}{2} \rfloor. For convenience, call such roads “Type-1 roads”, and call city ⌊i2⌋\lfloor \frac{i}{2} \rfloor the “parent city” of city ii.

In addition, there are mm directed roads. Suppose the ii-th road starts from city uiu_i and goes to city viv_i. Such roads have a special property: starting from city viv_i and repeatedly moving along Type-1 roads to the “parent city”, you can always eventually reach city uiu_i. Call such roads “Type-2 roads”.

Each road has a corresponding length. Therefore, for any two cities xx and yy in Country A, we can compute the minimum possible sum of road lengths along a route that starts from city xx and follows roads to reach city yy. Denote this value by dist(x,y)dist(x,y). However, due to serious flaws in Country A’s road system, it may be impossible to reach city yy from city xx. In this case, define dist(x,y)=0dist(x,y)=0. Also, traveling from a city to itself requires no roads, so define dist(x,x)=0dist(x,x)=0.

Now the administrators want to compute these values dist(x,y)dist(x,y) in order to reasonably measure how convenient trade is. But because there are too many cities, listing all these values one by one would be far too much work. Therefore, they only want the sum of all dist(x,y)dist(x,y), i.e. ∑x=12n−1∑y=12n−1dist(x,y)\sum_{x=1}^{2^n-1}{\sum_{y=1}^{2^n-1}{dist(x,y)}}, and they ask you to help.

Input Format

The first line contains two positive integers nn and mm.

The second line contains 2n−22^n-2 positive integers. The ii-th number aia_i denotes the length of the “Type-1 road” starting from city i+1i+1 and going to city ⌊i+12⌋\lfloor \frac{i+1}{2} \rfloor.

The next mm lines each contain three positive integers u,v,wu,v,w, describing a “Type-2 road” from city uu to city vv, with length ww.

Output Format

Output one line with one positive integer, the corresponding answer. Since the answer may be very large, you only need to output it modulo 998244353998244353.

2 1
2 1
1 2 2
8
见附件中的 trade/trade2.in。
见附件中的 trade/trade2.ans。
见附件中的 trade/trade3.in。
见附件中的 trade/trade3.ans。
见附件中的 trade/trade4.in。
见附件中的 trade/trade4.ans。

Hint

Constraints

For all testdata, it is guaranteed that: 2≤n≤182 \le n \le 18, 1≤m≤2n1 \le m \le 2 ^ n, 1≤u,v≤2n−11 \le u, v \le 2 ^ n - 1, 1≤ai,w≤1091 \le a_i, w \le 10 ^ 9.

::cute-table{tuack}

Test Point ID nn mm Whether There Is a Special Property
1∼21\sim 2 =8=8 ≤256\le 256 No
3∼43\sim 4 =9=9 ≤512\le 512 ^
5∼85\sim 8 =12=12 ≤4,096\le 4,096
99 =16=16 ≤10\le 10
1010 ^ ≤50\le 50
1111 ≤100\le 100
1212 ≤65,536\le 65,536 Yes
13∼1513\sim 15 ^ No
16∼1716\sim 17 =18=18 ≤262,144\le 262,144 Yes
18∼2018\sim 20 ^ No

Special property: It is guaranteed that every “Type-2 road” starts from the capital (city 11).

Translated by ChatGPT 5