You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones and an integer $$$k$$$. In one operation you can do one of the following:
Determine if it's possible to turn $$$a$$$ into $$$[1]$$$ after several (possibly zero) operations.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le k \le n \le 50$$$), the size of array $$$a$$$ and the length of segments that you can perform second type operation on.
The second line contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$.
For each test case, if it is possible to turn $$$a$$$ into $$$[1]$$$, print "YES", otherwise print "NO".
73 20 1 05 31 0 1 1 02 21 14 40 0 0 06 30 0 1 0 0 17 51 1 1 1 1 1 15 30 0 1 0 0
YES YES YES NO YES YES YES
In the first test case, you can perform the second type operation on second and third elements so $$$a$$$ becomes $$$[0, 1]$$$, then you can perform the second type operation on first and second elements, so $$$a$$$ turns to $$$[1]$$$.
In the fourth test case, it's obvious to see that you can't make any $$$1$$$, no matter what you do.
In the fifth test case, you can first perform a type 2 operation on the first three elements so that $$$a$$$ becomes $$$[1, 0, 0, 1]$$$, then perform a type 2 operation on the elements in positions two through four, so that $$$a$$$ becomes $$$[1, 1]$$$, and finally perform the first type operation on the remaining elements, so that $$$a$$$ becomes $$$[1]$$$.
You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones. You can do the following operation:
Note that elements of $$$a$$$ can become bigger than $$$1$$$ after performing some operations. Also note that $$$n$$$ becomes $$$1$$$ less after the operation.
What is the minimum number of operations needed to make $$$a$$$ non-decreasing, i. e. that each element is not less than the previous element?
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 each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the size of array $$$a$$$.
Next line contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$.
It's guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
For each test case print a single integer, minimum number of operations needed to make $$$a$$$ non-decreasing.
480 0 1 1 1 1 1 151 0 0 1 121 0111 1 0 0 1 0 0 1 1 1 0
0 1 1 3
In the first test case, $$$a$$$ is already non-decreasing, so you don't need to do any operations and the answer is $$$0$$$.
In the second test case, you can perform an operation for $$$i = 1$$$ and $$$j = 5$$$, so $$$a$$$ will be equal to $$$[0, 0, 1, 2]$$$ and it becomes non-decreasing.
In the third test case, you can perform an operation for $$$i = 2$$$ and $$$j = 1$$$, so $$$a$$$ will be equal to $$$[1]$$$ and it becomes non-decreasing.
You are given a permutation $$$a$$$ of size $$$n$$$ and you should perform $$$n$$$ operations on it. In the $$$i$$$-th operation, you can choose a non-empty suffix of $$$a$$$ and increase all of its elements by $$$i$$$. How can we perform the operations to minimize the number of inversions in the final array?
Note that you can perform operations on the same suffix any number of times you want.
A permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $$$a$$$ is a pair of indices $$$(i, j)$$$ such that $$$i \gt j$$$ and $$$a_{i} \lt a_{j}$$$.
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 each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the size of the array.
The second line contains $$$n$$$ distinct integers $$$a_{1}, a_{2}, \dots, a_{n}$$$ ($$$1 \le a_i \le n$$$), the initial permutation $$$a$$$.
It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print $$$n$$$ integers $$$x_{1}, x_{2}, \ldots, x_{n}$$$ ($$$1 \le x_{i} \le n$$$ for each $$$1 \le i \le n$$$) indicating that the $$$i$$$-th operation must be applied to the suffix starting at index $$$x_{i}$$$. If there are multiple answers, print any of them.
441 2 3 451 3 2 4 532 3 111
1 1 1 1 1 4 3 2 1 1 3 3 1
In the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $$$1$$$). The final array $$$[11, 12, 13, 14]$$$ contains $$$0$$$ inversions.
In the second test case, $$$a$$$ will be equal to $$$[2, 4, 3, 5, 6]$$$, $$$[2, 4, 3, 7, 8]$$$, $$$[2, 4, 6, 10, 11]$$$, $$$[2, 8, 10, 14, 15]$$$ and $$$[7, 13, 15, 19, 20]$$$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $$$a$$$ has zero inversions.
You are given a rooted tree consisting of $$$n$$$ vertices. The vertices are numbered from $$$1$$$ to $$$n$$$, and the root is the vertex $$$1$$$. You are also given a score array $$$s_1, s_2, \ldots, s_n$$$.
A multiset of $$$k$$$ simple paths is called valid if the following two conditions are both true.
It can be shown that it is always possible to find at least one valid multiset. Find the maximum value among all valid multisets.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two space-separated integers $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) and $$$k$$$ ($$$1 \le k \le 10^9$$$) — the size of the tree and the required number of paths.
The second line contains $$$n - 1$$$ space-separated integers $$$p_2,p_3,\ldots,p_n$$$ ($$$1\le p_i\le n$$$), where $$$p_i$$$ is the parent of the $$$i$$$-th vertex. It is guaranteed that this value describe a valid tree with root $$$1$$$.
The third line contains $$$n$$$ space-separated integers $$$s_1,s_2,\ldots,s_n$$$ ($$$0 \le s_i \le 10^4$$$) — the scores of the vertices.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10 ^ 5$$$.
For each test case, print a single integer — the maximum value of a path multiset.
25 41 2 1 36 2 1 5 75 31 2 1 36 6 1 4 10
54 56
In the first test case, one of optimal solutions is four paths $$$1 \to 2 \to 3 \to 5$$$, $$$1 \to 2 \to 3 \to 5$$$, $$$1 \to 4$$$, $$$1 \to 4$$$, here $$$c=[4,2,2,2,2]$$$. The value equals to $$$4\cdot 6+ 2\cdot 2+2\cdot 1+2\cdot 5+2\cdot 7=54$$$.
In the second test case, one of optimal solution is three paths $$$1 \to 2 \to 3 \to 5$$$, $$$1 \to 2 \to 3 \to 5$$$, $$$1 \to 4$$$, here $$$c=[3,2,2,1,2]$$$. The value equals to $$$3\cdot 6+ 2\cdot 6+2\cdot 1+1\cdot 4+2\cdot 10=56$$$.
The only difference between this problem and the hard version is the maximum number of questions.
This is an interactive problem.
There is a hidden integer $$$1 \le x \le n$$$ which you have to find. In order to find it you can ask at most $$$\mathbf{82}$$$ questions.
In each question you can choose a non-empty integer set $$$S$$$ and ask if $$$x$$$ belongs to $$$S$$$ or not, after each question, if $$$x$$$ belongs to $$$S$$$, you'll receive "YES", otherwise "NO".
But the problem is that not all answers are necessarily true (some of them are joking), it's just guaranteed that for each two consecutive questions, at least one of them is answered correctly.
Additionally to the questions, you can make at most $$$2$$$ guesses for the answer $$$x$$$. Each time you make a guess, if you guess $$$x$$$ correctly, you receive ":)" and your program should terminate, otherwise you'll receive ":(".
As a part of the joking, we will not fix the value of $$$x$$$ in the beginning. Instead, it can change throughout the interaction as long as all the previous responses are valid as described above.
Note that your answer guesses are always answered correctly. If you ask a question before and after a guess, at least one of these two questions is answered correctly, as normal.
The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the maximum possible value of $$$x$$$.
For each question, if you want to ask about a set $$$S$$$, first print the character '?', then print the size of $$$S$$$ and then print the elements of $$$S$$$ one by one. Each element should be an integer between $$$1$$$ and $$$n$$$, the elements must be distinct. After each question, read a string "YES" or "NO", as explained in the statement. You can make at most $$$82$$$ such questions.
If you want to guess for $$$x$$$, first print the character '!' and then print your guess. After each guess, read ":)" or ":(". If you guess $$$x$$$ correctly, the answer is ":)" and your program should terminate immediately, otherwise you'll receive ":(". You can make at most $$$2$$$ such guesses.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
Hacking is not allowed in this problem.
6
NO
:(
NO
:)
? 5 1 2 5 4 3
! 6
? 4 1 2 3 4
! 5
If the answer of the first question were correct, then $$$x$$$ would have been equal to $$$6$$$, but as we can see in the first guess, $$$6$$$ is not the answer.
So the answer of the first question is joking. As we know, the answer of at least one of our two questions is correct, since the answer of the first question was joking, the answer of the second question should be correct.
So we will understand that $$$x$$$ is not equal to $$$1, 2, 3$$$ or $$$4$$$, and we also knew that $$$x$$$ is not equal to $$$6$$$ either. Hence $$$x$$$ should be equal to $$$5$$$.
The only difference between this problem and the hard version is the maximum number of questions.
This is an interactive problem.
There is a hidden integer $$$1 \le x \le n$$$ which you have to find. In order to find it you can ask at most $$$\mathbf{53}$$$ questions.
In each question you can choose a non-empty integer set $$$S$$$ and ask if $$$x$$$ belongs to $$$S$$$ or not, after each question, if $$$x$$$ belongs to $$$S$$$, you'll receive "YES", otherwise "NO".
But the problem is that not all answers are necessarily true (some of them are joking), it's just guaranteed that for each two consecutive questions, at least one of them is answered correctly.
Additionally to the questions, you can make at most $$$2$$$ guesses for the answer $$$x$$$. Each time you make a guess, if you guess $$$x$$$ correctly, you receive ":)" and your program should terminate, otherwise you'll receive ":(".
As a part of the joking, we will not fix the value of $$$x$$$ in the beginning. Instead, it can change throughout the interaction as long as all the previous responses are valid as described above.
Note that your answer guesses are always answered correctly. If you ask a question before and after a guess, at least one of these two questions is answered correctly, as normal.
The only line of the input contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$), the maximum possible value of $$$x$$$.
For each question, if you want to ask about a set $$$S$$$, first print the character '?', then print the size of $$$S$$$ and then print the elements of $$$S$$$ one by one. Each element should be an integer between $$$1$$$ and $$$n$$$, the elements must be distinct. After each question, read a string "YES" or "NO", as explained in the statement. You can make at most $$$53$$$ such questions.
If you want to guess for $$$x$$$, first print the character '!' and then print your guess. After each guess, read ":)" or ":(". If you guess $$$x$$$ correctly, the answer is ":)" and your program should terminate immediately, otherwise you'll receive ":(". You can make at most $$$2$$$ such guesses.
After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
Hacking is not allowed in this problem.
6
NO
:(
NO
:)
? 5 1 2 5 4 3
! 6
? 4 1 2 3 4
! 5
If the answer of the first question were correct, then $$$x$$$ would have been equal to $$$6$$$, but as we can see in the first guess, $$$6$$$ is not the answer.
So the answer of the first question is joking. As we know, the answer of at least one of our two questions is correct, since the answer of the first question was joking, the answer of the second question should be correct.
So we will understand that $$$x$$$ is not equal to $$$1, 2, 3$$$ or $$$4$$$, and we also knew that $$$x$$$ is not equal to $$$6$$$ either. Hence $$$x$$$ should be equal to $$$5$$$.
You have an array $$$a$$$ consisting of $$$n$$$ positive integers and you have to handle $$$q$$$ queries of the following types:
The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$1 \le n , q \le 3 \cdot 10^5$$$), the length of $$$a$$$ and the number of queries.
Next line contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots a_{n}$$$ ($$$1 \le a_{i} \le 10^9$$$) — the elements of $$$a$$$.
Each of the next $$$q$$$ lines describes a query. It has one of the following forms.
For each query of the second type, if answer of the query is yes, print "YES", otherwise print "NO".
10 8
1234 2 3 3 2 1 1 2 3 4
2 1 6 2
1 1 1
2 1 6 2
2 1 9 2
1 10 5
2 1 9 3
1 3 5
2 3 10 2
NO
YES
NO
YES
YES
In the first query, requested subarray is $$$[1234, 2, 3, 3, 2, 1]$$$, and it's obvious that the number of occurrence of $$$1$$$ isn't divisible by $$$k = 2$$$. So the answer is "NO".
In the third query, requested subarray is $$$[1, 2, 3, 3, 2, 1]$$$, and it can be seen that the number of occurrence of every integer in this sub array is divisible by $$$k = 2$$$. So the answer is "YES".
In the sixth query, requested subarray is $$$[1, 2, 3, 3, 2, 1, 1, 2, 3]$$$, and it can be seen that the number of occurrence of every integer in this sub array is divisible by $$$k = 3$$$. So the answer is "YES".
Anton decided to get ready for an Olympiad in Informatics. Ilya prepared $$$n$$$ tasks for him to solve. It is possible to submit the solution for the $$$i$$$-th task in the first $$$d_{i}$$$ days only. Anton cannot solve more than one task a day. Ilya estimated the usefulness of the $$$i$$$-th tasks as $$$r_{i}$$$ and divided the tasks into three topics, the topic of the $$$i$$$-th task is $$$type_{i}$$$.
Anton wants to solve exactly $$$a$$$ tasks on the first topic, $$$b$$$ tasks on the second topic and $$$c$$$ tasks on the third topic. Tell Anton if it is possible to do so, and if it is, calculate the maximum total usefulness of the tasks he may solve.
The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.
The first line of each test case contains four integers $$$n, a, b, c$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le a, b, c \le n$$$).
The following $$$n$$$ lines contain three integers each — $$$r_i, type_i, d_i$$$ ($$$0 \le r_i \le 10^{9}$$$, $$$1 \le type_i \le 3$$$, $$$1 \le d_i \le n$$$).
The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case print $$$-1$$$ if Anton cannot reach his goal; otherwise, print the maximum usefulness of the tasks he will solve.
44 1 1 01 2 11 1 10 1 21 2 23 1 1 11 1 27 2 19 3 24 2 1 0100 2 15 2 37 1 25 1 25 1 1 110 3 19 2 320 1 116 1 41 3 4
2 -1 17 35
In the first test case from the sample test Anton can solve tasks $$$2$$$ and $$$4$$$.
In the second test case from the sample test it is impossible to fulfill Anton's wish.
In the third test case from the sample test it is optimal to solve tasks $$$2$$$, $$$3$$$ and $$$4$$$.
In the last test case from the sample test it is optimal to solve tasks $$$1$$$, $$$2$$$ and $$$4$$$.