TheForces Round #32 (2^5-Forces, TheForces Rated, Prizes!)
A. Short Query
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer array $$$a$$$ containing $$$n$$$ elements.

You can do the following operation on array $$$a$$$ :

  1. Choose two integers $$$a_{i}$$$, $$$a_{j}$$$ $$$(i ≠ j)$$$.
  2. If $$$(a_{i} + a_{j})$$$ is even, then remove $$$a_{i}$$$, $$$a_{j}$$$ from $$$a$$$ and add $$$(a_{i} + a_{j})$$$ at the end of $$$a$$$.

What are the minimum number of elements in $$$a$$$ after performing the above operation for at most $$$k$$$ times.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows.

The first line of test case contains two integers $$$n,k$$$ ($$$1 \le n,k \le 10^3$$$) — the length of the array $$$a$$$ and the number of operations performed.

The second line of each test case contains $$$n$$$ space separated integers $$$a_{i}$$$ ($$$1 \le a_{i} \le 10^3$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^3$$$.

Output

For each test case, print single integer — minimum number of elements in $$$a$$$ after performing the operation for at most $$$k$$$ times.

Example
Input
8
1 1
1
1 3
5
2 5
4 6
3 1
1 2 3
5 4
3 5 7 9 10
5 3
1 2 3 4 5
7 20
190 910 999 189 672 111 673
10 2
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
Output
1
1
1
2
1
2
1
8
Note

In the $$$5^{th}$$$ test case,

Initially, $$$a$$$ is $$$(3, 5, 7, 9, 10)$$$.

  1. After performing $$$1^{st}$$$ operation $$$a$$$ will be $$$(7, 9, 10, 8)$$$.
  2. After performing $$$2^{nd}$$$ operation $$$a$$$ will be $$$(10, 8, 16)$$$.
  3. After performing $$$3^{rd}$$$ operation $$$a$$$ will be $$$(16, 18)$$$.
  4. After performing $$$4^{th}$$$ operation $$$a$$$ will be $$$(34)$$$.

Hence, total $$$1$$$ element is there in $$$a$$$ after performing $$$4$$$ operations.

B. Minimum MEX
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer array $$$a$$$ containing $$$n$$$ elements.

A pair $$$(l, r) (1 \le l \le r \le n)$$$ is called Normal pair, if $$$^\dagger$$$ $$$MEX(a_{l},a_{l+1},....,a_{r-1},a_{r})$$$ has maximum value among all other pairs.

Out of all possible Normal pairs, find a pair $$$(l, r)$$$ which has a minimum value of $$$(r-l+1)$$$.

$$$^\dagger$$$ $$$MEX(b)$$$ is defined as the smallest non-negative integer which is not present in $$$b$$$. For example $$$MEX(1, 0, 3) = 2$$$, $$$MEX(2, 5, 3) = 0$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$.

The second line of each test case contains $$$n$$$ space separated integers $$$a_{i}$$$ ($$$0 \le a_{i} \le 10^9$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

Output

For each test case, print a single integer — the minimum value of $$$(r-l+1)$$$, such that $$$MEX(a_{l},a_{l+1},....,a_{r-1},a_{r})$$$ has maximum value.

Example
Input
5
4
3 3 0 1
5
1 1 2 0 0
6
5 0 3 5 1 1
6
10 11 23 22 9 3
8
7 4 2 4 0 7 2 1
Output
2
3
4
1
4
Note

In the $$$2^{nd}$$$ test case,

The possible Normal pairs are $$$(1, 4), (1, 5), (2, 4), (2, 5)$$$

So, for $$$l = 2, r = 4$$$, $$$MEX(1,2,0) = 3$$$ which is maximum.

Hence the smallest value of $$$(r-l+1) = 3$$$.

C. Range Contradiction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an integer array $$$a(a_{1},a_{2},....,a_{n})$$$containing $$$n$$$ elements, it is guaranteed that $$$n$$$ is even.

Let's define some definitions:

  • $$$S_{1}(a)$$$ is defined as set of elements in $$$a$$$ at odd index positions. i.e., $$$S_{1}(a) = (a_{1},a_{3},....,a_{n-1})$$$.
  • $$$S_{2}(a)$$$ is defined as set of elements in $$$a$$$ at even index positions. i.e., $$$S_{2}(a) = (a_{2},a_{4},....,a_{n})$$$.
  • $$$R(S)$$$ is defined as the Range$$$^\dagger$$$ of the set $$$S$$$.
  • $$$f(a) = R(S_{1}(a))*R(S_{2}(a))$$$

You are allowed to do the following operation on $$$a$$$:

  • Choose $$$i,j(1 \le i,j \le n)$$$ and swap $$$a_{i}$$$ and $$$a_{j}$$$.

Let the array $$$b$$$ be the resultant array $$$a$$$ after performing above operation any times(possibly zero).

Output any $$$b$$$ which has minimum $$$f(b)$$$.

$$$^\dagger$$$ Range of the set $$$S$$$ is defined as the difference between the maximum element and minimum element in $$$S$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of test case contains a single integers $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$, it is guaranteed thar $$$n$$$ is even.

The second line of each test case contains $$$n$$$ space separated integers $$$a_{i}$$$ ($$$1 \le a_{i} \le 10^9$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$.

Output

For each test case, print $$$n$$$ space seperated integers — the array $$$b$$$ which has minimum $$$f(b)$$$.

Example
Input
5
2
1 2
4
3 9 7 5
4
2 4 4 4
6
3 2 1 6 5 4
6
1000 1000 1000 1000 1000 1000
Output
2 1
9 5 7 3
2 4 4 4
3 5 1 6 2 4
1000 1000 1000 1000 1000 1000
Note

In the $$$4^{th}$$$ test case,

Let $$$b = (3,5,1,6,2,4)$$$

So, $$$S_{1}(b) = (3,1,2)$$$ and $$$S_{2}(b) = (5,6,4)$$$

so, $$$R(S_{1}(b)) = (3 - 1) = 2$$$ and $$$R(S_{2}(b)) = (6 - 4) = 2$$$

Hence, $$$f(b) = 2 * 2 = 4$$$.

It is shown that, there is no other $$$b$$$ where $$$f(b)$$$ is smaller than $$$4$$$.

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

You are given a binary string$$$^\dagger$$$ $$$S$$$ of length $$$n$$$.

For a given non-negative integer $$$m$$$ you can choose any binary string $$$P$$$ of length $$$n$$$ which contains $$$m$$$ $$$1's$$$ and $$$(n-m)$$$ $$$0's$$$.

Let the binary string $$$T = S \oplus P$$$, where $$$\oplus$$$ denotes Bitwise XOR

What are the maximum number of contiguous substrings of length $$$2$$$ in $$$T$$$ which are equal to $$$'11'$$$.

Solve the above problem for every $$$m(0 \le m \le n)$$$ .

$$$^\dagger$$$ A binary string is a string which only contains $$$0's$$$ and $$$1's$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — the length of the binary string.

The second line of each testcase contains a binary string $$$S$$$ of length $$$n$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^6$$$

Output

For each test case, print $$$n+1$$$ space seperated integers — the required answer for every $$$m(0 \le m \le n)$$$.

Example
Input
4
3
010
3
011
4
0011
5
11100
Output
0 1 2 0 
1 2 1 0 
1 2 3 2 1 
2 3 4 3 2 1 
Note

In the $$$1^{st}$$$ test case,

  1. For $$$m = 0$$$, let $$$P = 000$$$, then $$$T = 010 \oplus 000 = 010$$$, so total number of contiguous substrings of length $$$2$$$ in $$$T$$$ which are equal to $$$'11'$$$ are $$$0$$$.
  2. For $$$m = 1$$$, let $$$P = 001$$$, then $$$T = 010 \oplus 001 = 011$$$, so total number of contiguous substrings of length $$$2$$$ in $$$T$$$ which are equal to $$$'11'$$$ are $$$1$$$.
  3. For $$$m = 2$$$, let $$$P = 101$$$, then $$$T = 010 \oplus 101 = 111$$$, so total number of contiguous substrings of length $$$2$$$ in $$$T$$$ which are equal to $$$'11'$$$ are $$$2$$$.
  4. For $$$m = 3$$$, let $$$P = 111$$$, then $$$T = 010 \oplus 111 = 101$$$, so total number of contiguous substrings of length $$$2$$$ in $$$T$$$ which are equal to $$$'11'$$$ are $$$0$$$.

E. Not a Segment Tree
time limit per test
6 s
memory limit per test
512 MB
input
standard input
output
standard output

You are given two integer arrays $$$a(a_{0},a_{1},....,a_{n-1})$$$ containing $$$n$$$ elements, $$$b(b_{0},b_{1},....,b_{n-1})$$$ containing $$$n$$$ elements and an integer $$$k$$$.

You are allowed to do the following popular segment tree Operation on array $$$a$$$.

  1. Choose any value $$$l(0 \le l \le n-1)$$$.
  2. Let $$$sum$$$ = $$$(a_{(l-k)\%n}+a_{(l-k+1)\%n}+....+a_{l-1}+a_{l}+a_{l+1}+....+a_{(l+k-1)\%n}+a_{(l+k)\%n})$$$.
  3. Update $$$a_{l}$$$ = $$$sum$$$.

Find the minimum number of Operations required to change $$$a$$$ to $$$b$$$ or report if it is impossible to do.

Note that, $$$x\%y$$$ is the remainder when $$$y$$$ divides $$$x$$$. i.e., $$$(10\%4) = 2$$$, $$$(-5\%4) = 3$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.

The first line of test case contains two integers $$$n,k$$$ ($$$1 \le n \le 2 \cdot 10^6$$$, $$$0 \le k \le ⌊\frac{n-1}{2}⌋$$$) — the length of the given arrays and an integer.

The second line of each test case contains $$$n$$$ space separated integers $$$a_{i}$$$ ($$$1 \le a_{i} \le 10^{12}$$$).

The third line of each test case contains $$$n$$$ space separated integers $$$b_{i}$$$ ($$$1 \le b_{i} \le 10^{12}$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^6$$$.

Output

For each test case, print a single integer — the minimum number of Operations required to change $$$a$$$ to $$$b$$$ or $$$"-1"$$$(without quotes) if it is impossible to change.

Example
Input
3
4 1
1 4 3 2
17 14 25 2
6 2
1 1 1 1 1 1
1 49 5 29 9 13
5 2
1 2 3 9 11
2 4 6 18 22
Output
4
5
-1
Note

In the first test case,

  1. $$$a = (1, 4, 3, 2)$$$, by choosing $$$l = 2$$$ , $$$sum = 9$$$, so update $$$a_{2} = 9$$$, after this operation array will be $$$a = (1, 4, 9, 2)$$$.
  2. $$$a = (1, 4, 9, 2)$$$, by choosing $$$l = 1$$$ , $$$sum = 14$$$, so update $$$a_{1} = 14$$$, after this operation array will be $$$a = (1, 14, 9, 2)$$$.
  3. $$$a = (1, 14, 9, 2)$$$, by choosing $$$l = 2$$$ , $$$sum = 25$$$, so update $$$a_{2} = 25$$$, after this operation array will be $$$a = (1, 14, 25, 2)$$$.
  4. $$$a = (1, 14, 25, 2)$$$, by choosing $$$l = 0$$$ , $$$sum = 17$$$, so update $$$a_{0} = 17$$$, after this operation array will be $$$a = (17, 14, 25, 2)$$$, which is equal to $$$b$$$.

Hence, the total operations required are $$$4$$$.

F1. Award from Wuhudsm(Easy Version)
time limit per test
7 s
memory limit per test
256 megabytes
input
standard input
output
standard output

This is the easy version of the problem. The changes in this version are highlighted in bold characters.

Yugandhar have an undirected graph containing $$$n$$$ nodes numbered $$$1,2,....,n$$$. Initially there are $$$0$$$ edges i.e., there are total $$$n$$$ connected components in the graph. He has a freedom to add exactly $$$k$$$ undirected edges in this graph, each edge can be added between $$$u$$$ and $$$v$$$ if and only if $$$v$$$ = $$$u+1$$$. Note that he can add multiple edges between two nodes.

After giving this graph to wuhudsm, he will observe the following $$$m$$$ requirements and reward him coins :

  • If wuhudsm can able to reach $$$y_{i}$$$ from $$$x_{i}$$$ then he will reward him $$$c_{i}$$$ coins.

So Please tell him the optimal way to select these $$$k$$$ edges to get maximum number of coins.

Aditionally, help him for $$$q$$$ queries.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of test case contains three integers $$$n,m,q$$$ ($$$2 \le n \le \bf{2 \cdot 10^3}$$$, $$$1 \le m \le \bf{2 \cdot 10^3}$$$, $$$1 \le q \le 10^6$$$) — the number of nodes in graph, the number of requirements and the number of queries.

Next $$$m$$$ lines of each test case contains three space separated integers $$$x_{i}, y_{i}, c_{i}$$$ ($$$1 \le x_{i}, y_{i} \le n$$$, $$$x_{i} ≠ y_{i}$$$, $$$1 \le c_{i} \le 10^9$$$).

Next $$$q$$$ lines of each test case contains a single integer $$$k_{i}$$$ ($$$1 \le k_{i} \le 10^9$$$) — the total number of edges needed to add in graph.

It is guaranteed that the sum of $$$n$$$ and sum of $$$m$$$ over all test cases doesn't exceed $$$\bf{2 \cdot 10^3}$$$.

It is guaranteed that the sum of $$$q$$$ over all test cases doesn't exceed $$$10^6$$$.

Output

For each test case print $$$q$$$ integers — the maximum number of coins he can get if he choose exactly $$$k_{i}$$$ edges optimally.

Example
Input
3
4 2 2
3 4 3
1 3 2
1
3
5 3 3
3 4 2
3 1 5
2 5 10
2
1
6
6 5 4
1 2 1
3 2 1
3 4 1
5 4 2
6 5 1
3
4
8
10
Output
3
5
5
2
17
4
5
6
6

F2. Award from Wuhudsm(Hard Version)
time limit per test
7 s
memory limit per test
256 megabytes
input
standard input
output
standard output

This is the hard version of the problem. The changes in this version are highlighted in bold characters.

Yugandhar have an undirected graph containing $$$n$$$ nodes numbered $$$1,2,....,n$$$. Initially there are $$$0$$$ edges i.e., there are total $$$n$$$ connected components in the graph. He has a freedom to add exactly $$$k$$$ undirected edges in this graph, each edge can be added between $$$u$$$ and $$$v$$$ if and only if $$$v$$$ = $$$u+1$$$. Note that he can add multiple edges between two nodes.

After giving this graph to wuhudsm, he will observe the following $$$m$$$ requirements and reward him coins :

  • If wuhudsm can able to reach $$$y_{i}$$$ from $$$x_{i}$$$ then he will reward him $$$c_{i}$$$ coins.

So Please tell him the optimal way to select these $$$k$$$ edges to get maximum number of coins.

Aditionally, help him for $$$q$$$ queries.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^4$$$). The description of the test cases follows.

The first line of test case contains three integers $$$n,m,q$$$ ($$$2 \le n \le \bf{10^4}$$$, $$$1 \le m \le \bf{10^4}$$$, $$$1 \le q \le 10^6$$$) — the number of nodes in graph, the number of requirements and the number of queries.

Next $$$m$$$ lines of each test case contains three space separated integers $$$x_{i}, y_{i}, c_{i}$$$ ($$$1 \le x_{i}, y_{i} \le n$$$, $$$x_{i} ≠ y_{i}$$$, $$$1 \le c_{i} \le 10^9$$$).

Next $$$q$$$ lines of each test case contains a single integer $$$k_{i}$$$ ($$$1 \le k_{i} \le 10^9$$$) — the total number of edges needed to add in graph.

It is guaranteed that the sum of $$$n$$$ and sum of $$$m$$$ over all test cases doesn't exceed $$$\bf{10^4}$$$.

It is guaranteed that the sum of $$$q$$$ over all test cases doesn't exceed $$$10^6$$$.

Output

For each test case print $$$q$$$ integers — the maximum number of coins he can get if he choose exactly $$$k_{i}$$$ edges optimally.

Example
Input
3
4 2 2
3 4 3
1 3 2
1
3
5 3 3
3 4 2
3 1 5
2 5 10
2
1
6
6 5 4
1 2 1
3 2 1
3 4 1
5 4 2
6 5 1
3
4
8
10
Output
3
5
5
2
17
4
5
6
6