You are given an undirected graph with vertices and edges.
Each vertex has an initial bit a_i, equal to or , and a nonnegative cost w_i for changing it.
You may change the vertex bits. If the final bit of vertex differs from its initial bit, you must pay w_i.
Each edge has a color from to and a number , equal to or .
An edge between vertices and imposes the constraint x_u \oplus x_v=b, where x_u and x_v are the final bits of the corresponding vertices, and denotes the bitwise XOR operation.
For each color , independently perform the following steps:
Remove all edges of color from the graph.
Choose the final bits of all vertices so that the constraint of every remaining edge is satisfied.
Minimize the total cost of changing the bits.
Find the minimum cost for each color separately. If it is impossible to satisfy all remaining constraints, the answer for that color is .
All cases are independent: before removing the next color, the graph and the initial vertex bits are restored to their original state.
The first line contains three integers , , and (1 \leq n \leq 2 \cdot 10^5, 0 \leq m \leq 2 \cdot 10^5, 1 \leq k \leq 2 \cdot 10^5) — the number of vertices, edges, and colors.
The second line contains a binary string of length , where a_i is the initial bit of vertex .
The third line contains integers w_1,w_2,\ldots,w_n (0 \leq w_i \leq 10^9) — the costs of changing the bits.
Each of the next lines contains four integers , , , and (1 \leq u,v \leq n, b \in \{0,1\}, 1 \leq c \leq k). This describes an edge of color between vertices and imposing the constraint x_u \oplus x_v=b.
The graph may contain parallel edges and self-loops. Some colors may not occur among the edges.
Print integers.
The integer at position must be the minimum cost after removing all edges of color , or if the remaining constraints cannot all be satisfied.
Example 1
Example 2
Example 3
In the first example, after removing the edges of colors , , and , you can choose the final bits 100, 110, and 000, respectively. The minimum costs are 5+2=7, , and .
In the second example, removing color leaves two identical constraints x_1=x_2. It is enough to change the bit of the first vertex at a cost of . Removing color or color , which is absent from the graph, leaves a self-loop requiring x_1 \oplus x_1=1, which is impossible.
In the third example, there are no edges. For each color, all initial bits can be left unchanged, so both answers are .
points — k=1.
points — n \leq 16, m \leq 30, k \leq 10.
points — n,m,k \leq 2000.
points — k \leq 20.
points — b=0 for every edge.
points — w_i=0 for every vertex.
points — no additional constraints.