QOJ.ac

QOJ

时间限制: 1.5 s 内存限制: 512 MB 总分: 100 难度: [显示]

#6758. Trade

统计

In recent years, the trade of Country A has developed rapidly, but the domestic road construction has not kept pace, which has clearly become a limitation on trade exchanges, causing the administrators much concern.

Specifically, Country A has $2^n - 1$ cities, with city 1 being the capital. For every non-capital city $i$, there is a one-way road from city $i$ to city $\lfloor \frac{i}{2} \rfloor$. For convenience, such roads are called "Type 1 roads," and city $\lfloor \frac{i}{2} \rfloor$ is called the "superior city" of city $i$.

In addition, there are $m$ one-way roads. Let the $i$-th such road go from city $u_i$ to city $v_i$. These roads have a special property: starting from city $v_i$ and continuously moving towards the "superior city" along Type 1 roads, one can always eventually reach city $u_i$. Such roads are called "Type 2 roads."

Each road has a corresponding length. Thus, for any two cities $x$ and $y$ in Country A, one can calculate the minimum sum of lengths of the roads traversed to travel from city $x$ to city $y$, denoted as $dist(x, y)$. However, due to severe defects in the road construction of Country A, it may be impossible to reach city $y$ from city $x$. For convenience, we define $dist(x, y) = 0$ in this case. Also, no roads need to be traversed to travel from a city to itself, so we define $dist(x, x) = 0$.

Now, the administrators wish to calculate these $dist(x, y)$ values to reasonably measure the convenience of trade exchanges. However, because the number of cities in Country A is too large, the workload of listing these values one by one is too great. Therefore, the administrators only wish to find the sum of all $dist(x, y)$ values, which is $\sum_{x=1}^{2^n-1} \sum_{y=1}^{2^n-1} dist(x, y)$, and they hope for your help.

Input

The first line contains two positive integers $n$ and $m$.

The second line contains $2^n - 2$ positive integers, where the $i$-th number $a_i$ represents the length of the "Type 1 road" from city $i+1$ to city $\lfloor \frac{i+1}{2} \rfloor$.

The next $m$ lines each contain three positive integers $u, v, w$, describing a "Type 2 road" from city $u$ to city $v$ with length $w$.

Output

Output a single positive integer representing the answer. Since the answer may be very large, you only need to output the answer modulo $998244353$.

Constraints

For all test data, it is guaranteed that $2 \le n \le 18$, $1 \le m \le 2^n$, $1 \le u, v \le 2^n-1$, and $1 \le a_i, w \le 10^9$.

Test Case ID $n$ $m$ Special Property
$1 \sim 2$ $= 8$ $\le 256$ No
$3 \sim 4$ $= 9$ $\le 512$ No
$5 \sim 8$ $= 12$ $\le 4,096$ No
$9$ $\le 10$ No
$10$ $\le 50$ No
$11$ $\le 100$ No
$12$ $\le 65,536$ Yes
$13 \sim 15$ $\le 65,536$ No
$16 \sim 17$ $\le 262,144$ Yes
$18 \sim 20$ $\le 262,144$ No

Special property: It is guaranteed that every "Type 2 road" starts from the capital (city 1).

Examples

Input 1

2 1
2 1
1 2 2

Output 1

8

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.