After completing his HSC exams, Antar decides that he has had enough of studying and wants to live a peaceful life. He moves to a quiet village in Akibland and starts his very own chicken farm.
However, Antar soon realizes that farming isn't as peaceful as he imagined.
Antar has $$$n$$$ food boxes arranged in a line. The $$$i$$$-th box contains $$$A[i]$$$ units of food.
For each of his $$$q$$$ queries, Antar wants to feed a group of $$$k$$$ chickens, where every chicken has a different appetite. He is given a ratio $$$B[1], B[2], \dots, B[k]$$$ describing how the food should be divided among these $$$k$$$ chickens.
The chickens must receive their food in order: the 1st chicken must receive its food before the 2nd, the 2nd before the 3rd, and so on. The food boxes must also be used in their original order — Antar cannot rearrange them. Therefore, all $$$n$$$ boxes must be split into $$$k$$$ contiguous, non-overlapping segments (covering every box, in order), and the $$$i$$$-th segment is given entirely to the $$$i$$$-th chicken.
Let $$$B'[i]$$$ be the total amount of food actually received by the $$$i$$$-th chicken under such a split. Antar wants this split to satisfy, for every $$$1 \le i, j \le k$$$:
$$$$$$ \frac{B'[i]}{B'[j]} = \frac{B[i]}{B[j]} $$$$$$
In other words, the food received by the chickens must be split in exactly the ratio $$$B[1] : B[2] : \dots : B[k]$$$.
For each query, determine whether such a split is possible. If it is, report where each chicken's segment ends.
Constraints
The input is read from standard input in the following format:
For each query, output:
If it is impossible to split all $$$n$$$ boxes into $$$k$$$ contiguous segments (covering all boxes, in order) whose totals are in the ratio $$$B[1] : B[2] : \dots : B[k]$$$, output the following.
If it is possible, output the following.
| Subtask | Points | $$$\boldsymbol{n}$$$ | $$$\boldsymbol{q}$$$ | $$$\boldsymbol{k}$$$ | Additional Constraints |
| 0 | 0 | – | – | – | The examples |
| 1 | 5 | $$$\le 1000$$$ | $$$=1$$$ | $$$=2$$$ | No additional constraints. |
| 2 | 10 | $$$\le 1000$$$ | – | $$$=3$$$ | $$$\sum k \le 1000$$$ |
| 3 | 15 | $$$\le 10^5$$$ | – | $$$=2$$$ | No additional constraints. |
| 4 | 30 | $$$\le 10^5$$$ | – | $$$=3$$$ | No additional constraints. |
| 5 | 40 | $$$\le 10^5$$$ | $$$\le 10^5$$$ | $$$\le n$$$ | No additional constraints. |
6 51 12 13 13 51 131 2 432 1 431 1 534 2 131 38 52
YES 2 4 6 YES 3 4 6 YES 2 3 6 NO YES 1 4 6
Consider the first query: $$$$$$ 1 : 2 : 4 $$$$$$
Antar can divide the boxes as follows:
Therefore, $$$$$$ 13 : 26 : 52 = 1 : 2 : 4. $$$$$$
So the distribution is possible, and the last box received by each chicken is:
2 4 6
Hence the answer is:
YES
2 4 6
For the fourth query ($$$4 : 2 : 1$$$), no way of splitting the $$$6$$$ boxes into $$$3$$$ contiguous, ordered segments produces totals in that exact ratio, so the answer is NO.
The days of jokes are over!
Our beloved leader Pepe has become the supreme leader of Pepeland. As his first great decision, he has decided to eliminate all the noobs and fools from Pepeland. Only the smartest ones may remain!
To find out who is truly smart enough to stay, Pepe decides to create the riddle of the century. For this, he seeks the help of the smartest person on Earth, Woumya.
Pepe takes Woumya to a huge dungeon in Pepeland, where Dali and Sesh are trapped. The dungeon consists of $$$n$$$ rooms connected by $$$n - 1$$$ bidirectional corridors, forming a tree structure (a connected graph with no cycles). Each room has a number written on it, where the number written on room $$$i$$$ is $$$A_i$$$.
A path in the dungeon from room $$$u$$$ to room $$$v$$$ is defined as a sequence of distinct rooms $$$p_1,p_2,\ldots,p_m$$$ such that $$$p_1=u$$$, $$$p_m=v$$$, and $$$p_i$$$ and $$$p_{i+1}$$$ are directly connected by a corridor for all $$$1 \leq i \lt m$$$.
Woumya can choose any path through the dungeon. Suppose the path contains the rooms $$$u_1,u_2,\ldots,u_k$$$ in this order.
The beauty of this path is defined as
$$$$$$ \sum_{i=1}^{k} \gcd(A_{u_1},A_{u_2},\ldots,A_{u_i}). $$$$$$
Woumya wants to impress Pepe by finding the path with the maximum possible beauty. A path containing only a single room is also allowed.
Your task is to calculate the maximum possible beauty.
The input is read from standard input in the following format:
Each test file contains multiple test cases. The first line of the input contains the number of test cases $$$T$$$. Then, for each test case, the input is given in the following format:
For each test case, output a single integer — the maximum possible beauty of any route through the dungeon.
Constraints
| Subtask | Points | $$$\boldsymbol{n}$$$ | $$$\boldsymbol{A_i}$$$ | Additional Constraints |
| 0 | 0 | – | – | The examples |
| 1 | 10 | $$$\le 10^5$$$ | $$$\le 10^4$$$ | Every room is directly connected to a room $$$r$$$ by a corridor where $$$1 \le r \le n$$$. |
| 2 | 15 | $$$\le 1000$$$ | $$$\le 10^4$$$ | No additional constraints. |
| 3 | 75 | $$$\le 10^5$$$ | $$$\le 10^4$$$ | No additional constraints. |
1429 26 19 281 22 33 4
32
167 10 2 15 9 61 21 31 53 43 6
18
364669 9654 8331 3885 9651 78061 22 33 44 51 61089 23 34 72 41 3 60 99 48 971 21 33 44 52 66 73 82 97 10479 26 19 781 22 33 4
9663 105 82
Example 1
Input
1
4
29 26 19 28
1 2
2 3
3 4
Output
32
Explanation
The optimal path is from room $$$1$$$ to $$$4$$$ where the rooms are $$$1, 2, 3, 4$$$ in order. Hence, the maximum beauty is $$$\gcd(A_1) + \gcd(A_1, A_2) + \gcd(A_1, A_2, A_3) + \gcd(A_1, A_2, A_3, A_4) = \gcd(29) + \gcd(29,26) + \gcd(29,26,19) + \gcd(29,26,19,28) = 32.$$$
Example 2
Input
1
6
7 10 2 15 9 6
1 2
1 3
1 5
3 4
3 6
Output
18
Explanation
The optimal path is from room $$$4$$$ to room $$$2$$$, where the rooms are $$$4,3,1,2$$$ in order. Hence, the maximum beauty is
The optimal path is from room $$$4$$$ to $$$2$$$ where the rooms are $$$4, 3, 1, 2$$$ in order. Hence, the maximum beauty is $$$\gcd(A_4) + \gcd(A_4, A_3) + \gcd(A_4. A_3, A_1) + \gcd(A_4, A_3, A_1, A_2)=\gcd (15) + \gcd ( 15, 2) + \gcd(15, 2, 7) + \gcd(15,2,7,10) = 18.$$$
Turjo is managing the front counter at Akibdonalds. He has exactly $$$N$$$ pagers on his charging rack, numbered from $$$1$$$ to $$$N$$$.
A group of $$$k$$$ customers ($$$k \ge 1$$$) must receive $$$k$$$ consecutive pager IDs so that Turjo can carry them on one tray.
For every arriving group, Turjo chooses the smallest possible starting ID $$$l$$$ such that all pagers
$$$$$$ l,l+1,\dots,l+k-1 $$$$$$
are currently available.
When the customers' food is ready, they return their pagers to Turjo, making those pagers available again. The returned pagers always form a consecutive range.
You are given a sequence of $$$Q$$$ events in chronological order. Simulate Turjo's actions and determine which pager IDs each arriving group receives.
The first line contains two integers $$$N$$$ and $$$Q$$$.
The next $$$Q$$$ lines describe the following events.
It is guaranteed that $$$1 \leq k \leq N$$$.
It is guaranteed that $$$1 \leq l \leq r \leq N$$$, and every pager in this range is currently being held by a customer.
All group arrivals can be fulfilled using the currently available pagers.
Print the answers in chronological order.
Constraints
| Subtask | Points | $$$\boldsymbol{N}$$$ | $$$\boldsymbol{Q}$$$ | $$$\boldsymbol{k}$$$ | Additional Constraints |
| 0 | 0 | – | – | – | The examples |
| 1 | 5 | $$$=1$$$ | $$$\le 200000$$$ | $$$=1$$$ | All events involve the only pager. |
| 2 | 15 | $$$\le 5000$$$ | $$$\le 5000$$$ | $$$1 \le k \le N$$$ | No additional constraints. |
| 3 | 15 | $$$\le 200000$$$ | $$$\le 200000$$$ | $$$=1$$$ | There are no Type 2 queries. |
| 4 | 25 | $$$\le 200000$$$ | $$$\le 200000$$$ | $$$\le 3$$$ | No additional constraints. |
| 5 | 40 | $$$\le 200000$$$ | $$$\le 200000$$$ | $$$1 \le k \le N$$$ | No additional constraints. |
6 71 11 22 1 11 22 4 51 31 1
1 2 3 4 5 4 6 1
Examples
Example 1
Input
6 7
1 1
1 2
2 1 1
1 2
2 4 5
1 3
1 1
Output
1
2 3
4 5
4 6
1
Explanation
There are $$$6$$$ pagers, numbered from $$$1$$$ to $$$6$$$.
We process the events one by one:
Pagers $$$2$$$ and $$$3$$$ are the smallest consecutive available pagers, so they receive $$$2,3$$$.
Pager $$$1$$$ becomes available again.
Pagers $$$4$$$ and $$$5$$$ are the smallest consecutive available pagers, so they receive $$$4,5$$$.
Therefore, both pagers $$$4$$$ and $$$5$$$ become available again.
At this moment, the available pagers are
[ 1,4,5,6. ]
The smallest three consecutive available pagers are $$$4,5,6$$$, so the group receives $$$4,5,6$$$.
Therefore, we print:
4 6
Pager $$$1$$$ is the smallest available pager, so the customer receives pager $$$1$$$.
Thus the complete output is:
1
2 3
4 5
4 6
1