Vasya has a pile, that consists of some number of stones. $$$n$$$ times he either took one stone from the pile or added one stone to the pile. The pile was non-empty before each operation of taking one stone from the pile.
You are given $$$n$$$ operations which Vasya has made. Find the minimal possible number of stones that can be in the pile after making these operations.
The first line contains one positive integer $$$n$$$ — the number of operations, that have been made by Vasya ($$$1 \leq n \leq 100$$$).
The next line contains the string $$$s$$$, consisting of $$$n$$$ symbols, equal to "-" (without quotes) or "+" (without quotes). If Vasya took the stone on $$$i$$$-th operation, $$$s_i$$$ is equal to "-" (without quotes), if added, $$$s_i$$$ is equal to "+" (without quotes).
Print one integer — the minimal possible number of stones that can be in the pile after these $$$n$$$ operations.
3 ---
0
4 ++++
4
2 -+
1
5 ++-++
3
In the first test, if Vasya had $$$3$$$ stones in the pile at the beginning, after making operations the number of stones will be equal to $$$0$$$. It is impossible to have less number of piles, so the answer is $$$0$$$. Please notice, that the number of stones at the beginning can't be less, than $$$3$$$, because in this case, Vasya won't be able to take a stone on some operation (the pile will be empty).
In the second test, if Vasya had $$$0$$$ stones in the pile at the beginning, after making operations the number of stones will be equal to $$$4$$$. It is impossible to have less number of piles because after making $$$4$$$ operations the number of stones in the pile increases on $$$4$$$ stones. So, the answer is $$$4$$$.
In the third test, if Vasya had $$$1$$$ stone in the pile at the beginning, after making operations the number of stones will be equal to $$$1$$$. It can be proved, that it is impossible to have less number of stones after making the operations.
In the fourth test, if Vasya had $$$0$$$ stones in the pile at the beginning, after making operations the number of stones will be equal to $$$3$$$.
Let's call an array of non-negative integers $$$a_1, a_2, \ldots, a_n$$$ a $$$k$$$-extension for some non-negative integer $$$k$$$ if for all possible pairs of indices $$$1 \leq i, j \leq n$$$ the inequality $$$k \cdot |i - j| \leq min(a_i, a_j)$$$ is satisfied. The expansion coefficient of the array $$$a$$$ is the maximal integer $$$k$$$ such that the array $$$a$$$ is a $$$k$$$-extension. Any array is a 0-expansion, so the expansion coefficient always exists.
You are given an array of non-negative integers $$$a_1, a_2, \ldots, a_n$$$. Find its expansion coefficient.
The first line contains one positive integer $$$n$$$ — the number of elements in the array $$$a$$$ ($$$2 \leq n \leq 300\,000$$$). The next line contains $$$n$$$ non-negative integers $$$a_1, a_2, \ldots, a_n$$$, separated by spaces ($$$0 \leq a_i \leq 10^9$$$).
Print one non-negative integer — expansion coefficient of the array $$$a_1, a_2, \ldots, a_n$$$.
4 6 4 5 5
1
3 0 1 2
0
4 821 500 479 717
239
In the first test, the expansion coefficient of the array $$$[6, 4, 5, 5]$$$ is equal to $$$1$$$ because $$$|i-j| \leq min(a_i, a_j)$$$, because all elements of the array satisfy $$$a_i \geq 3$$$. On the other hand, this array isn't a $$$2$$$-extension, because $$$6 = 2 \cdot |1 - 4| \leq min(a_1, a_4) = 5$$$ is false.
In the second test, the expansion coefficient of the array $$$[0, 1, 2]$$$ is equal to $$$0$$$ because this array is not a $$$1$$$-extension, but it is $$$0$$$-extension.
$$$n$$$ boys and $$$m$$$ girls came to the party. Each boy presented each girl some integer number of sweets (possibly zero). All boys are numbered with integers from $$$1$$$ to $$$n$$$ and all girls are numbered with integers from $$$1$$$ to $$$m$$$. For all $$$1 \leq i \leq n$$$ the minimal number of sweets, which $$$i$$$-th boy presented to some girl is equal to $$$b_i$$$ and for all $$$1 \leq j \leq m$$$ the maximal number of sweets, which $$$j$$$-th girl received from some boy is equal to $$$g_j$$$.
More formally, let $$$a_{i,j}$$$ be the number of sweets which the $$$i$$$-th boy give to the $$$j$$$-th girl. Then $$$b_i$$$ is equal exactly to the minimum among values $$$a_{i,1}, a_{i,2}, \ldots, a_{i,m}$$$ and $$$g_j$$$ is equal exactly to the maximum among values $$$b_{1,j}, b_{2,j}, \ldots, b_{n,j}$$$.
You are interested in the minimum total number of sweets that boys could present, so you need to minimize the sum of $$$a_{i,j}$$$ for all $$$(i,j)$$$ such that $$$1 \leq i \leq n$$$ and $$$1 \leq j \leq m$$$. You are given the numbers $$$b_1, \ldots, b_n$$$ and $$$g_1, \ldots, g_m$$$, determine this number.
The first line contains two integers $$$n$$$ and $$$m$$$, separated with space — the number of boys and girls, respectively ($$$2 \leq n, m \leq 100\,000$$$). The second line contains $$$n$$$ integers $$$b_1, \ldots, b_n$$$, separated by spaces — $$$b_i$$$ is equal to the minimal number of sweets, which $$$i$$$-th boy presented to some girl ($$$0 \leq b_i \leq 10^8$$$). The third line contains $$$m$$$ integers $$$g_1, \ldots, g_m$$$, separated by spaces — $$$g_j$$$ is equal to the maximal number of sweets, which $$$j$$$-th girl received from some boy ($$$0 \leq g_j \leq 10^8$$$).
If the described situation is impossible, print $$$-1$$$. In another case, print the minimal total number of sweets, which boys could have presented and all conditions could have satisfied.
3 2 1 2 1 3 4
12
2 2 0 1 1 0
-1
2 3 1 0 1 1 2
4
In the first test, the minimal total number of sweets, which boys could have presented is equal to $$$12$$$. This can be possible, for example, if the first boy presented $$$1$$$ and $$$4$$$ sweets, the second boy presented $$$3$$$ and $$$2$$$ sweets and the third boy presented $$$1$$$ and $$$1$$$ sweets for the first and the second girl, respectively. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to $$$12$$$.
In the second test, the boys couldn't have presented sweets in such way, that all statements satisfied.
In the third test, the minimal total number of sweets, which boys could have presented is equal to $$$4$$$. This can be possible, for example, if the first boy presented $$$1$$$, $$$1$$$, $$$2$$$ sweets for the first, second, third girl, respectively and the second boy didn't present sweets for each girl. It's easy to see, that all conditions are satisfied and the total number of sweets is equal to $$$4$$$.
Let $$$s$$$ be some string consisting of symbols "0" or "1". Let's call a string $$$t$$$ a substring of string $$$s$$$, if there exists such number $$$1 \leq l \leq |s| - |t| + 1$$$ that $$$t = s_l s_{l+1} \ldots s_{l + |t| - 1}$$$. Let's call a substring $$$t$$$ of string $$$s$$$ unique, if there exist only one such $$$l$$$.
For example, let $$$s = $$$"1010111". A string $$$t = $$$"010" is an unique substring of $$$s$$$, because $$$l = 2$$$ is the only one suitable number. But, for example $$$t = $$$"10" isn't a unique substring of $$$s$$$, because $$$l = 1$$$ and $$$l = 3$$$ are suitable. And for example $$$t =$$$"00" at all isn't a substring of $$$s$$$, because there is no suitable $$$l$$$.
Today Vasya solved the following problem at the informatics lesson: given a string consisting of symbols "0" and "1", the task is to find the length of its minimal unique substring. He has written a solution to this problem and wants to test it. He is asking you to help him.
You are given $$$2$$$ positive integers $$$n$$$ and $$$k$$$, such that $$$(n \bmod 2) = (k \bmod 2)$$$, where $$$(x \bmod 2)$$$ is operation of taking remainder of $$$x$$$ by dividing on $$$2$$$. Find any string $$$s$$$ consisting of $$$n$$$ symbols "0" or "1", such that the length of its minimal unique substring is equal to $$$k$$$.
The first line contains two integers $$$n$$$ and $$$k$$$, separated by spaces ($$$1 \leq k \leq n \leq 100\,000$$$, $$$(k \bmod 2) = (n \bmod 2)$$$).
Print a string $$$s$$$ of length $$$n$$$, consisting of symbols "0" and "1". Minimal length of the unique substring of $$$s$$$ should be equal to $$$k$$$. You can find any suitable string. It is guaranteed, that there exists at least one such string.
4 4
1111
5 3
01010
7 3
1011011
In the first test, it's easy to see, that the only unique substring of string $$$s = $$$"1111" is all string $$$s$$$, which has length $$$4$$$.
In the second test a string $$$s = $$$"01010" has minimal unique substring $$$t =$$$"101", which has length $$$3$$$.
In the third test a string $$$s = $$$"1011011" has minimal unique substring $$$t =$$$"110", which has length $$$3$$$.
Vasya has written some permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, so for all $$$1 \leq i \leq n$$$ it is true that $$$1 \leq p_i \leq n$$$ and all $$$p_1, p_2, \ldots, p_n$$$ are different. After that he wrote $$$n$$$ numbers $$$next_1, next_2, \ldots, next_n$$$. The number $$$next_i$$$ is equal to the minimal index $$$i \lt j \leq n$$$, such that $$$p_j \gt p_i$$$. If there is no such $$$j$$$ let's let's define as $$$next_i = n + 1$$$.
In the evening Vasya went home from school and due to rain, his notebook got wet. Now it is impossible to read some written numbers. Permutation and some values $$$next_i$$$ are completely lost! If for some $$$i$$$ the value $$$next_i$$$ is lost, let's say that $$$next_i = -1$$$.
You are given numbers $$$next_1, next_2, \ldots, next_n$$$ (maybe some of them are equal to $$$-1$$$). Help Vasya to find such permutation $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, that he can write it to the notebook and all numbers $$$next_i$$$, which are not equal to $$$-1$$$, will be correct.
The first line contains one integer $$$t$$$ — the number of test cases ($$$1 \leq t \leq 100\,000$$$).
Next $$$2 \cdot t$$$ lines contains the description of test cases,two lines for each. The first line contains one integer $$$n$$$ — the length of the permutation, written by Vasya ($$$1 \leq n \leq 500\,000$$$). The second line contains $$$n$$$ integers $$$next_1, next_2, \ldots, next_n$$$, separated by spaces ($$$next_i = -1$$$ or $$$i \lt next_i \leq n + 1$$$).
It is guaranteed, that the sum of $$$n$$$ in all test cases doesn't exceed $$$500\,000$$$.
In hacks you can only use one test case, so $$$T = 1$$$.
Print $$$T$$$ lines, in $$$i$$$-th of them answer to the $$$i$$$-th test case.
If there is no such permutations $$$p_1, p_2, \ldots, p_n$$$ of integers from $$$1$$$ to $$$n$$$, that Vasya could write, print the only number $$$-1$$$.
In the other case print $$$n$$$ different integers $$$p_1, p_2, \ldots, p_n$$$, separated by spaces ($$$1 \leq p_i \leq n$$$). All defined values of $$$next_i$$$ which are not equal to $$$-1$$$ should be computed correctly $$$p_1, p_2, \ldots, p_n$$$ using defenition given in the statement of the problem. If there exists more than one solution you can find any of them.
6 3 2 3 4 2 3 3 3 -1 -1 -1 3 3 4 -1 1 2 4 4 -1 4 5
1 2 3 2 1 2 1 3 -1 1 3 2 1 4
In the first test case for permutation $$$p = [1, 2, 3]$$$ Vasya should write $$$next = [2, 3, 4]$$$, because each number in permutation is less than next. It's easy to see, that it is the only satisfying permutation.
In the third test case, any permutation can be the answer because all numbers $$$next_i$$$ are lost.
In the fourth test case, there is no satisfying permutation, so the answer is $$$-1$$$.
Vasya has $$$n$$$ different points $$$A_1, A_2, \ldots A_n$$$ on the plane. No three of them lie on the same line He wants to place them in some order $$$A_{p_1}, A_{p_2}, \ldots, A_{p_n}$$$, where $$$p_1, p_2, \ldots, p_n$$$ — some permutation of integers from $$$1$$$ to $$$n$$$.
After doing so, he will draw oriented polygonal line on these points, drawing oriented segments from each point to the next in the chosen order. So, for all $$$1 \leq i \leq n-1$$$ he will draw oriented segment from point $$$A_{p_i}$$$ to point $$$A_{p_{i+1}}$$$. He wants to make this polygonal line satisfying $$$2$$$ conditions:
Vasya has a string $$$s$$$, consisting of $$$(n-2)$$$ symbols "L" or "R". Let's call an oriented polygonal line winding, if its $$$i$$$-th turn left, if $$$s_i = $$$ "L" and right, if $$$s_i = $$$ "R". More formally: $$$i$$$-th turn will be in point $$$A_{p_{i+1}}$$$, where oriented segment from point $$$A_{p_i}$$$ to point $$$A_{p_{i+1}}$$$ changes to oriented segment from point $$$A_{p_{i+1}}$$$ to point $$$A_{p_{i+2}}$$$. Let's define vectors $$$\overrightarrow{v_1} = \overrightarrow{A_{p_i} A_{p_{i+1}}}$$$ and $$$\overrightarrow{v_2} = \overrightarrow{A_{p_{i+1}} A_{p_{i+2}}}$$$. Then if in order to rotate the vector $$$\overrightarrow{v_1}$$$ by the smallest possible angle, so that its direction coincides with the direction of the vector $$$\overrightarrow{v_2}$$$ we need to make a turn counterclockwise, then we say that $$$i$$$-th turn is to the left, and otherwise to the right. For better understanding look at this pictures with some examples of turns:
There are left turns on this picture
There are right turns on this picture You are given coordinates of the points $$$A_1, A_2, \ldots A_n$$$ on the plane and string $$$s$$$. Find a permutation $$$p_1, p_2, \ldots, p_n$$$ of the integers from $$$1$$$ to $$$n$$$, such that the polygonal line, drawn by Vasya satisfy two necessary conditions.
The first line contains one integer $$$n$$$ — the number of points ($$$3 \leq n \leq 2000$$$). Next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$, divided by space — coordinates of the point $$$A_i$$$ on the plane ($$$-10^9 \leq x_i, y_i \leq 10^9$$$). The last line contains a string $$$s$$$ consisting of symbols "L" and "R" with length $$$(n-2)$$$. It is guaranteed that all points are different and no three points lie at the same line.
If the satisfying permutation doesn't exists, print $$$-1$$$. In the other case, print $$$n$$$ numbers $$$p_1, p_2, \ldots, p_n$$$ — the permutation which was found ($$$1 \leq p_i \leq n$$$ and all $$$p_1, p_2, \ldots, p_n$$$ are different). If there exists more than one solution, you can find any.
3 1 1 3 1 1 3 L
1 2 3
6 1 0 0 1 0 2 -1 0 -1 -1 2 1 RLLR
6 1 3 4 2 5
This is the picture with the polygonal line from the $$$1$$$ test:
As we see, this polygonal line is non-self-intersecting and winding, because the turn in point $$$2$$$ is left.
This is the picture with the polygonal line from the $$$2$$$ test: