NOI '23 P4 - Trade

View as PDF

Submit solution

Points: 20 (partial)
Time limit: 1.5s
Memory limit: 512M

Problem type

In recent years, the commercial development in country A has been rapid, but the domestic road construction has not kept up with the pace, which has clearly become a restriction on people's trade and has caused great concern for the managers.

Specifically, there are 2n1 cities in country A, where city 1 is the capital. For all non-capital cities i, there is a single one-way road that starts from city i and reaches city i2. For convenience, these roads are called "first-class roads", and the city i2 is called the "superior city" of city i.

In addition, there are m directed roads, where the i-th road goes from city ui to city vi, and they all have a special property: starting from city vi and continuously walking along the first-class roads towards the "superior city", one can always reach city ui. These roads are called "second-class roads".

Each road has a corresponding length. Therefore, for any two cities x and y in country A, one can calculate the minimum value of the sum of the lengths of the roads passed through from city x to city y, and this value is denoted as dist(x,y). However, due to serious defects in the road construction in country A, it may be impossible to reach city y from city x. For convenience, dist(x,y) is defined as 0 in this case. At the same time, a city does not need to pass through any road to reach itself, so dist(x,x) is defined as0.

Now, the managers hope to calculate these dist(x,y) values in order to measure the convenience of people's trade. However, since there are too many cities in country A, listing all these values one by one is too time-consuming. Therefore, the managers only hope to find the sum of all dist(x,y) values, that is, i=12n1y=12n1dist(x,y), and they hope that you can help.

Since the answer may be very large, you only need to output the answer modulo 998244353.

Input Specification

The first line of input contains two positive integers n and m.

The second line of input contains 2n2 positive integers. The i-th integer ai represents the length of the "first-class road" from city i+1 to city i+12.

Then m lines follow, each containing three positive integers u,v and w, representing a "second-class road" from city u to city v with length w.

Output Specification

Output a line containing an integer, representing the answer, taken modulo 998244353.

Sample Input 1

Copy
2 1
2 1
1 2 2

Sample Output 1

Copy
8

Additional Samples

Sample inputs and outputs can be found here.

  • Sample 2 (ex_trade2.in and ex_trade2.ans).
  • Sample 3 (ex_trade3.in and ex_trade3.ans).
  • Sample 4 (ex_trade4.in and ex_trade4.ans).

Constraints

For all test data, it is guaranteed that: 2n18,1m2n,1u,v2n1,1ai,w109.

Test IDnmAdditional Constraints
12=8 256None
34=9512
58=124096
9=1610
1050
11100
1265536A
1315None
1617=18262144A
1820None

Additional Constraint A: It is guaranteed that every "second-class road" starts from the capital (city 1).


Comments

There are no comments at the moment.