2024 USACO.Guide Informatics Tournament
A. TriNum Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In the vibrant town of Numerica, where numbers ruled the marketplace, a clever mathematician named Alaric presented a challenge. Traders were tasked with finding the longest stretch of numbers in an array, but here's the catch – they could only use up to three different numbers in that stretch. It was like a numerical puzzle, a quest to uncover the magic of the longest subarray with just three types of numbers.

As traders delved into Numerica, each subarray they discovered was a chapter in their adventure, revealing the delicate dance of numbers within Alaric's constraints. The goal was simple yet intriguing: for an array $$$A$$$ find the longest subarray with at most three different numbers, and earn the admiration of the entire marketplace.

Input
  1. The first line contains an integer, $$$N$$$, representing the length of the array. $$$(1\le N \le 10^5)$$$
  2. The second line contains $$$N$$$ space-separated integers, representing $$$A$$$ ($$$1 \le A_i \le 2 \cdot 10^5$$$).
Output

Output a single integer representing the length of the longest subarray where traders, armed with arrays, successfully unravel the numerical puzzle presented by Alaric.

Example
Input
7
1 4 2 1 9 1 10
Output
4

B. Two Way Homework
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two arrays $$$A$$$ and $$$B$$$ of length $$$N$$$. For each position $$$i$$$, we need to choose one element either $$$A_i$$$ or $$$B_i$$$ and the goal is to choose values in such a way that the sum of the values is as small as possible. Note that we choose elements in increasing order of $$$i$$$.

However, there is a catch as the problem would be way too easy otherwise. Every time we take a value from array $$$A$$$, the cost of every subsequent value we choose will double.

For example, if we have the arrays:

$$$[4, 1, 3, 4]$$$ $$$[7, 2, 1, 3]$$$

If we choose $$$4$$$ from the first array, this means that for the next $$$3$$$ positions, the true costs will be $$$(2, 6, 8)$$$ and $$$(4, 2, 6)$$$ respectively which can be further doubled as well.

Note that if we take an element from $$$B$$$ this doubling process does not happen.

Input

The first line of the input will contain $$$N$$$, the length of the arrays ($$$1 \leq N \leq 10^5$$$).

The second line of the input will contain the array $$$A$$$ ($$$1 \leq A_i \leq 2 \cdot 10^9$$$).

The third line of the input will contain the array $$$B$$$ ($$$1 \leq B_i \leq 2 \cdot 10^9$$$).

Output

You have to print the minimum sum of the values we can obtain according to the rules of the problem.

Example
Input
2
1 3
4 1
Output
3

C. Balanced Difference
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an array $$$a$$$ of $$$n$$$ elements, you want to partition $$$a$$$ into $$$2$$$ subsequences of size at least $$$2$$$ such that every element in the array belongs to exactly one subsequence and the difference between the maximum and minimum element in each subsequence is the same. Print the maximum possible difference or $$$-1$$$ if a partition cannot be found.

Input

The first line includes one number $$$n$$$ ($$$2 \le n \le 10^5$$$), the length of the array. The second line contains $$$n$$$ space separated numbers representing $$$a$$$ ($$$-10^9 \le a_i \le 10^9$$$).

Output

Print one number representing the maximum difference of a valid partition.

Example
Input
5
4 7 2 9 5
Output
5
Note

In the example, we can divide the array into $$$[2, 5, 7]$$$ and $$$[4, 9]$$$. The difference between the maximum and minimum in each partition is equal to $$$5$$$.

D. Producing Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

For each test case, you are given arrays $$$a$$$ and $$$b$$$ with $$$n$$$ integers. For each $$$a_i$$$ determine whether you can multiply $$$a_i$$$ by any subset of the previous elements in the array (subset may be empty, meaning you do not multiply) such that the ones (unit) digit of the product is the digit $$$b_i$$$.

When working with negative numbers, the units digit still refers to the last digit when writing down the number. (The units digit of $$$-19$$$ is $$$9$$$)

Input

The first line gives the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The first line of each test case gives $$$n$$$, the size of the array ($$$1 \le n \le 10^5$$$). The sum of all $$$n$$$ is less than $$$n \le 5 \cdot 10^5$$$. The second line of each test case gives $$$n$$$ integers, $$$a_i$$$ ($$$-10^9 \le a_i \le 10^9$$$). The third line of each test case gives $$$n$$$ integers, $$$b_i$$$ ($$$0 \le b_i \le 9$$$).

Output

For each test case, print a string of length $$$n$$$ consisting of $$$N$$$s and $$$Y$$$s. For each element $$$a_i$$$, on the $$$i$$$-th character use a $$$Y$$$ if it is possible to create a product with the correct units digit or an $$$N$$$ if it is not possible.

Example
Input
2
5
1 2 3 4 5
2 2 2 3 4
10
3 3 3 -3 3 3 3 -3 -3 3
1 1 1 1 2 3 4 5 6 7
Output
NYNNN
NNNYNYNNNY

E. Gardening is Hard
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You have an $$$n$$$ by $$$n$$$ square garden which can be thought of as a grid. In each cell, there is either good planting land, a well, or bad planting land. You only want to focus on one gardening strip because it's much easier to maintain. A gardening strip is a $$$2$$$-tall strip that is always horizontal (but can have a variable length), where if you cut out just the gardening strip, all planting land must be at most $$$k$$$ away from the closest well, and all land in the strip is either good planting land or wells. Distance between two cells is defined by: $$$|a.x - b.x| + |a.y - b.y|$$$ (Manhattan distance). Now when trying to choose a gardening strip, you ask yourself, how many gardening strips are in my garden square?

Input

You will be given $$$n$$$ ($$$1 \le n \le 2000$$$) and $$$k$$$ ($$$1 \le k \le n$$$) in the first line. Then in the next $$$n$$$ lines, you will be given $$$n$$$ space-separated integers from $$$1$$$ to $$$3$$$. $$$1$$$ represents good planting land, $$$2$$$ represents a well and $$$3$$$ represents bad planting land.

Additionally, it is guaranteed there are no vertically adjacent wells. In order words, for every well not in the last row, the cell in the next row and the same column is not a well.

Output

Output the number of possible gardening strips.

Example
Input
10 3
1 1 1 1 2 1 1 3 1 1
2 1 1 1 1 1 1 2 1 3
1 1 2 1 1 2 1 3 1 1
1 1 1 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 2 1
1 1 1 2 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 2
1 2 1 1 1 3 1 1 1 1
1 3 1 1 1 1 1 2 1 3
1 1 3 2 1 1 1 1 1 3
Output
140
Note

Example of a good gardening strip:

$$$1$$$ $$$1$$$ $$$1$$$ $$$1$$$ $$$2$$$

$$$2$$$ $$$1$$$ $$$1$$$ $$$1$$$ $$$1$$$

All planting land (all the $$$1$$$s) is at most $$$2$$$ away from the wells (the $$$2$$$s), which is less than $$$k$$$ (which is $$$3$$$).

Example of a bad gardening strip:

$$$1$$$ $$$1$$$ $$$1$$$

$$$1$$$ $$$1$$$ $$$1$$$

There are no wells in the strip, and so none of the planting land is at most $$$k$$$ away from wells.

F. Farmer John's Cities
time limit per test
1 second
memory limit per test
250 megabytes
input
standard input
output
standard output

In the country of Farmer John, there exist $$$N$$$ cities and $$$M$$$ roads connecting some of these cities. We assume that the roads are directional and that the road going from city $$$u$$$ to city $$$v$$$ has a non-negative length $$$L(u,v)$$$.

A new road is to be built, and therefore, you are given a list of $$$K$$$ pairs of cities that this road could connect. Each proposed road from city $$$u$$$ to city $$$v$$$ is accompanied by its corresponding length $$$L(u,v)$$$.

You are also given two cities, $$$s$$$ and $$$t$$$. Your goal is to choose at most one of the proposed roads to achieve the minimum distance from city $$$s$$$ to city $$$t$$$ and print that distance.

Input

The first line contains five positive integers, $$$N$$$ $$$(1 \leq N \leq 10^4)$$$, $$$M$$$ $$$(1 \leq M \leq 10^5)$$$, $$$K$$$ $$$(1 \leq K \leq 10^4)$$$, $$$s$$$, and $$$t$$$ $$$(1 \leq s,t \leq N)$$$ respectively. Each of the following $$$M$$$ $$$(1\leq M \leq 10^5)$$$ lines describes a road and contains three integers, $$$u$$$, $$$v$$$, $$$(1 \leq u,v \leq N)$$$ and $$$L$$$ $$$(1 \leq L \leq 10^6)$$$ respectively. A directed road from city $$$u$$$ to city $$$v$$$, and the length of that road. The following $$$K$$$ lines describe the proposed roads, each containing again three integers, the start of the road, the end of the road, and its length respectively.

Output

You must print a single non-negative integer, the minimum distance possible after building one of the $$$K$$$ proposed roads.

Example
Input
4 4 2 2 4
1 3 10
2 1 7
4 2 9
3 4 8
2 3 15
1 4 12
Output
19
Note

If none of the proposed roads achieve a smaller distance, then you should just print the smallest distance in the already existing network.

G. Soccer League
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The soccer league started and our friend, B.B.C. wants to show the entire world he's the best soccer statistician in the world. Soccer is played as a game between two teams and the team who scores more goals wins. In case of equality, we will consider the game to end in a draw.

For this reason, he gave you the results of $$$T$$$ teams playing in different leagues and he wants you to tell him if for a given tuple $$$(W, D, L, G_f, G_a)$$$, where $$$W$$$ is the number of wins, $$$D$$$ is the number of draws, $$$L$$$ is the number of losses, $$$G_f$$$ is the numbers of goals scored and $$$G_a$$$ is the number of goals conceded, there is a unique set of results that corresponds to the tuple given in the input.

For example, if we have the tuple $$$(1, 1, 1, 1, 1)$$$, we have a unique set of results that lead us here $$$(1-0, 0-1, 0-0)$$$, the order these results show isn't important, however if we have $$$(1, 1, 1, 2, 2)$$$, we can have $$$(2-0, 0-2, 0-0)$$$ or $$$(1-0, 0-1, 1-1)$$$, so the set of results is not unique.

Now you need to show B.B.C. what you're made of and conquer this challenge!

Take note that if there is no valid way of assigning scores, you should also print "Better luck next time".

Input

On the first line you are given $$$T$$$, the number of test cases $$$(1 \le T \le 10^5$$$).

On the next $$$T$$$ lines you are given $$$W$$$, $$$D$$$, $$$L$$$, $$$G_f$$$, $$$G_a$$$, with the meaning from the statement. ($$$0 \le W, D, L, G_f, G_a \le 10^9$$$).

Output

You will need to print on each line either the message "Amazing" if the set of valid results is unique, or "Better luck next time" otherwise.

Example
Input
5
3 1 1 4 1
4 0 0 6 0
0 5 0 1 2
0 3 0 1 1
1 1 0 2 1
Output
Amazing
Better luck next time
Better luck next time
Amazing
Better luck next time

H. Cheating the Group System
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In math class, the teacher is trying a new grouping system. Every day he gives each of his $$$n \cdot 10^{100}$$$ students a card with some number from $$$1$$$ to $$$n$$$, to create $$$n$$$ random groups of equal size $$$10^{100}$$$. This means that he hands out $$$10^{100}$$$ cards with the number $$$1$$$, $$$10^{100}$$$ cards with the number $$$2$$$, ...., and $$$10^{100}$$$ cards with the number $$$n$$$. Each pair of students are either best friends, friends, or not friends. The issue is that students refuse to work in a group without all their best friends (they are okay working without their normal friends), and so they start trading their cards.

The students are socially awkward and only trade their cards with their normal friends and best friends. You are given $$$m$$$ pairs of students, each pair denoting two best friends who must be in the same group (students do not start with any normal friendships). Your job is to compute the minimum number of normal friendships that must be made before the students know their cards such that no matter what cards the students get, after trading any number of times, each pair of best friends has the same cards. Note that if two students are best or normal friends, they can trade infinitely many times.

Input

You will be given $$$n$$$ ($$$2 \leq n \leq 10^9$$$), and $$$m$$$ ($$$1 \leq m \leq 2 \cdot 10^5$$$) in the first line. The next $$$m$$$ lines contain two numbers $$$a$$$ and $$$b$$$ ($$$1 \leq a, b \leq 2 \cdot 10^5$$$), denoting that $$$a$$$ and $$$b$$$ are best friends.

Output

The minimum number of new friendships (not including best friendships).

Example
Input
3 3
1 2
2 3
10 11
Output
3
Note

For the sample case: If we create $$$3 - 4$$$, $$$4 - 5$$$, $$$5 - 10$$$ as new friendships, no matter how the cards are distributed, it can be proved that there exists a sequence of trades such that each of the pairs of best friends will always end up having the same numbered card.

As an example let student $$$1$$$ have card $$$1$$$, student $$$2$$$ have card $$$2$$$, student $$$3$$$ have card $$$3$$$, student $$$4$$$ have card $$$1$$$, student $$$5$$$ have card $$$2$$$, student $$$10$$$ have card $$$3$$$, and student $$$11$$$ have card $$$1$$$ (the rest do not matter).

start - $$$[1, 2, 3, 1, 2, 3, 1]$$$ (each element in the array represents the card of the student in the array $$$[1, 2, 3, 4, 5, 10, 11]$$$)

Here are the trades to satisfy the best friends:

$$$4$$$ swap $$$3$$$ - $$$[1, 2, 1, 3, 2, 3, 1]$$$

$$$3$$$ swap $$$2$$$ - $$$[1, 1, 2, 3, 2, 3, 1]$$$

$$$11$$$ swap $$$10$$$ - $$$[1, 1, 2, 3, 2, 1, 3]$$$

$$$10$$$ swap $$$5$$$ - $$$[1, 1, 2, 3, 1, 2, 3]$$$

$$$5$$$ swap $$$4$$$ - $$$[1, 1, 2, 1, 3, 2, 3]$$$

$$$4$$$ swap $$$3$$$ - $$$[1, 1, 1, 2, 3, 2, 3]$$$

$$$5$$$ swap $$$10$$$ - $$$[1, 1, 1, 2, 2, 3, 3]$$$

Now you can see that all of the best friend's conditions are satisfied.

I. Hori and Cake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hori-san is eating a yummy cake baked for her by Miyamura. The cake is a strange cake that can be represented by a collection of line segments. Hori will take one or more bites in order to eat the cake.

In any bite, Hori can choose any segment and eat that segment as well as any segments that are fully contained within the segment.

It is guaranteed that all endpoints of segments are distinct. In addition, if two segments intersect, it is guaranteed that one of them will fully contain the other.

How many different sequences of bites are there for Hori to eat the entire cake (no segments left) modulo $$$10^9+7$$$?

Input

The first line has a single integer $$$T$$$, ($$$1 \le T \le 5 \cdot 10^3$$$) the number of test cases.

The first line of each test case has a single integer $$$N$$$, ($$$1 \le N \le 5 \cdot 10^3$$$). The next $$$N$$$ lines contain $$$l_i$$$ and $$$r_i$$$, $$$1 \le l_i \lt r_i \le 2N$$$, denoting the left and right endpoints of the segments. It is guaranteed that the sum of $$$N$$$ over all test cases won't exceed $$$5 \cdot 10^3$$$.

Output

For each test case, a single integer representing the number of sequences modulo $$$10^9 + 7$$$.

Example
Input
6
1
1 2
2
1 4
2 3
2
1 2
3 4
3
1 2
3 6
4 5
3
1 6
2 5
3 4
19
5 6
15 16
26 35
29 32
11 12
22 23
27 28
14 25
8 9
30 31
17 20
1 36
37 38
21 24
3 10
4 7
33 34
18 19
2 13
Output
1
2
2
5
4
585868918
Note

In the first test case, since there is only $$$1$$$ segment, there is only $$$1$$$ sequence of bites Hori can take to eat all segments.

In the second test case, Hori can either eat $$$[[1, 4]]$$$ or $$$[[2, 3], [1, 4]]$$$. In the first sequence, eating $$$[1, 4]$$$ would also make $$$[2, 3]$$$ disappear.

In the fifth test case, Hori can eat the following sequences.

  1. $$$[[3, 4], [2, 5], [1, 6]]$$$
  2. $$$[[3, 4], [1, 6]]$$$
  3. $$$[[2, 5], [1, 6]]$$$
  4. $$$[[1, 6]]$$$

J. CRP Game
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Scientists recently invented a new game called CRP to test the IQ of people, as well as push them past their limits. CRP is a single player game that can be played using $$$2$$$ boards and a pen.

Assume that the two boards are named $$$A$$$ and $$$B$$$. You are given a sequence of $$$N$$$ numbers, each one belonging to the set $$$\{0,1,2,3\}$$$. The sequence is currently written on board $$$A$$$. In order to win the game, you need to move all numbers from board $$$A$$$ to board $$$B$$$ with the smallest amount of moves, such that all numbers with equal values are adjacent to each other on board $$$B$$$.

To move a number from $$$A$$$ to $$$B$$$, you are allowed to make use of the following operations described below:

1. Operation $$$C$$$: This operation is denoted by the letter $$$c$$$. In this operation, you can replace every number on board $$$A$$$ with its complement. For example, $$$x$$$ becomes $$$3-x$$$.

2. Operation $$$R$$$: This operation is denoted by the letter $$$r$$$. In this operation, you can reverse the order of the numbers on board $$$B$$$. For example, $$$\{1, 3, 0, 3\}$$$ becomes $$$\{3, 0, 3, 1\}$$$.

3.Operation $$$P$$$: This operation is denoted by the letter $$$p$$$. In this operation, you can take the first number from $$$A$$$ and push it to the end of $$$B$$$. For instance, if $$$A = \{1,3,3\}$$$ and $$$B = \{1,2,2\}$$$ before, then after performing the operation $$$P$$$ one time, $$$A = \{3,3\}$$$ and $$$B = \{1,2,2,1\}$$$.

The scientists decided to hold a tournament to popularize the game and find the people with the highest IQ. They have also agreed to give a big prize to each person of the tournament that successfully wins a game.

The tournament consists of $$$T$$$ CRP games. In each of the $$$T$$$ games, only one player is chosen and allowed to play the game. If he wins the game, he gets a big prize and a chance to meet with the scientists; otherwise, he doesn't get anything.

Let the string $$$S_i$$$ denote the sequence of operations $$$(c,r,p)$$$ with the smallest length, such that after performing the respective operations, the player currently chosen to play wins the $$$i$$$-th game.

Your task is to find and print $$$S_i$$$ for each $$$1 \le i \le T$$$.

If there exists more than one possible $$$S_i$$$ with the same length, then you need to find the lexicographically smallest one.

Input

The first line contains two integers $$$T$$$ $$$(1 \le T \le 20)$$$ and $$$N$$$ $$$(1 \le N \le 10^5)$$$, denoting the total number of games in the tournament and the length of the sequence given in each game, respectively.

Each of the next $$$Τ$$$ lines contains $$$N$$$ space-separated integers $$$A_i$$$, where $$$A_i \in \{0,1,2,3\}$$$, the sequence written on board $$$A$$$ at the start of each of the $$$T$$$ games.

It is guaranteed that the sum of $$$N$$$ over all testcases does not exceed $$$10^5$$$.

Output

For each of the $$$T$$$ games, print the sequence of operations $$$(c,r,p)$$$ with the smallest size, such that after performing the respective operations, the player currently chosen to play wins the game. If there exists more than one possible answer, then print the lexicographically smallest one.

Example
Input
6 5
1 3 1 0 2
1 1 3 0 2
0 3 3 0 3
2 1 1 3 2
1 0 3 0 1
1 1 3 1 3
Output
pprppp
ppppp
pcppcpp
pcpppp
ppcppp
pppcpp
Note

It is guaranteed that there is always a possible sequence of movements to win the game, thus there is always an answer.

A player is said to win a game only if he can accomplish the goal of the game, which is to move all numbers from board $$$A$$$ to board $$$B$$$ using the least amount of operations described above, such that all numbers with equal values are adjacent to each other on board $$$B$$$.

K. Counting Pairs
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the quest of trying to be the best student ever, you pose your teacher an interesting question. You begin with two numbers, $$$x$$$ and $$$y$$$ both equal to $$$1$$$. You can do two operations, operation $$$1$$$ is to set $$$x := x + y$$$, and operation $$$2$$$ is to set $$$y := x + y$$$. The task is to count how many ways (modulo $$$10^9 + 7$$$) you can do operation $$$1$$$ or $$$2$$$ until $$$ax + by \gt c$$$. Two ways are different if either the number of operations is different or in one way you do operation $$$1$$$ instead of operation $$$2$$$. Because your teacher cannot solve the problem, you decide to help him out and solve it on your own.

Input

First line will contain a number $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^5$$$).

The next $$$t$$$ lines will each contain three numbers number $$$a$$$, $$$b$$$, and $$$c$$$ ($$$1 \leq a,b,c \leq 2 \cdot 10^5$$$)

The sum of all $$$c$$$ does not exceed $$$10^6$$$

Output

For each of the $$$t$$$ testcases, print out the number of ways modulo $$$10^9 + 7$$$ for the given $$$a$$$, $$$b$$$, and $$$c$$$ in a new line.

Example
Input
7
1 1 4
1 1 5
1 2 5
2 3 5
3 3 5
1 1 100000
2 3 100000
Output
6
10
5
2
1
39650733
506608485
Note

For the first test case, we start at $$$(1, 1)$$$ (a xy pair will be represented as a coordinate).

Then we can go to in one operation: $$$(1, 2)$$$, $$$(2, 1)$$$

In two operations: $$$(3, 2)$$$, $$$(2, 3)$$$, $$$(1, 3)$$$, $$$(3, 1)$$$

Note that $$$(3, 2)$$$ and $$$(2, 3)$$$ already finished.

In three operations: $$$(4, 3)$$$, $$$(3, 4)$$$, $$$(1, 4)$$$, $$$(4, 1)$$$

All $$$4$$$ of these finished too.

This means that the answer is $$$6$$$.

L. Modulo Queries
time limit per test
8 s
memory limit per test
1024 MB
input
standard input
output
standard output

You are given an array $$$A$$$ of size $$$N$$$ and $$$Q$$$ queries.

In each query, you will be given three integers: $$$l$$$, $$$r$$$, and $$$x$$$. Find the value of

$$$$$$

(A_l \text{ mod } x) + (A_{l + 1} \text{ mod } x) + \dots + (A_{r - 1} \text{ mod } x) + (A_r \text{ mod } x)

$$$$$$

Note that $$$(x \text{ mod } y)$$$ denotes the remainder when $$$x$$$ is divided by $$$y$$$.

Input

The first line contains two integers $$$N$$$ and $$$Q$$$ ($$$1\leq N, Q\leq 2\cdot 10^5$$$) — the length of the array and the number of queries.

The second line contains $$$N$$$ integers $$$A_1, A_2, \dots, A_N$$$ ($$$1\leq A_i\leq 2\cdot 10^5$$$) — the elements of the array $$$A$$$.

Each of the next $$$Q$$$ lines contains three integers $$$l$$$, $$$r$$$, and $$$x$$$ ($$$1\leq l\leq r\leq N$$$, $$$1\leq x\leq 2\cdot 10^5$$$) — descriptions of the queries.

Output

Print $$$Q$$$ integers — the answers to the queries.

Examples
Input
7 10
14 16 9 6 9 10 16
5 6 3
2 2 7
5 7 5
4 6 4
7 7 7
7 7 16
2 6 15
6 6 12
7 7 11
3 7 2
Output
1 2 5 5 2 0 35 10 5 2
Input
7 10
8 18 6 15 4 18 15
5 7 2
1 1 18
4 5 3
3 5 12
6 7 10
5 5 14
4 4 1
4 4 19
7 7 12
6 6 4
Output
1 8 1 13 13 4 0 15 3 2
Input
7 10
14 8 8 12 7 10 15
5 5 10
1 4 5
2 4 6
2 3 17
5 6 9
3 7 15
6 6 11
7 7 10
7 7 20
2 7 17
Output
7 12 4 16 8 37 10 5 15 60
Input
7 10
11 9 18 5 11 14 10
1 7 18
3 6 4
7 7 16
4 4 5
5 6 12
4 6 16
7 7 2
2 3 5
3 7 4
2 4 19
Output
60 8 10 0 13 30 0 7 10 32