text
stringlengths
0
801
You only have time to perform $k$ operations before Sparkle sets off a nuclear bomb! What is the maximum score you can acquire after $k$ operations?
The first line contains $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains $n$ and $k$ ($1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^9$) — the length of the arrays and the number of operations you can perform.
The following line contains $n$ integers $a_1, a_2, ... a_n$ ($1 \leq a_i \leq 10^9$).
The following line contains $n$ integers $b_1, b_2, ... b_n$ ($1 \leq b_i \leq 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
For each test case, output an integer, the maximum score you can acquire after $k$ operations.
On Penacony, The Land of the Dreams, there exists $n$ houses and $n$ roads. There exists a road between house $i$ and $i+1$ for all $1 \leq i \leq n-1$ and a road between house $n$ and house $1$. All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt and may not be able to maintain all roads.
There are $m$ pairs of friendships between the residents of Penacony. If the resident living in house $a$ is friends with the resident living in house $b$, there must be a path between houses $a$ and $b$ through maintained roads.
What is the minimum number of roads that must be maintained?
The first line contains $t$ ($1 \leq t \leq 10^4$) – the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5$) – the number of houses and the number of friendships.
The next $m$ lines contain two integers $a$ and $b$ ($1 \leq a < b \leq n$) – the resident in house $a$ is friends with the resident in house $b$. It is guaranteed all ($a, b$) are distinct.
It is guaranteed the sum of $n$ and $m$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output an integer, the minimum number of roads that must be maintained.
For the first test case, the following roads must be maintained:
* $8 \leftarrow \rightarrow 1$ * $7 \leftarrow \rightarrow 8$ * $1 \leftarrow \rightarrow 2$ * $4 \leftarrow \rightarrow 5$
Monocarp's current password on Codeforces is a string $s$, consisting of lowercase Latin letters. Monocarp thinks that his current password is too weak, so he wants to insert exactly one lowercase Latin letter into the password to make it stronger. Monocarp can choose any letter and insert it anywhere, even before the first character or after the last character.
Monocarp thinks that the password's strength is proportional to the time it takes him to type the password. The time it takes to type the password is calculated as follows:
* the time to type the first character is $2$ seconds; * for each character other than the first, the time it takes to type it is $1$ second if it is the same as the previous character, or $2$ seconds otherwise.
For example, the time it takes to type the password abacaba is $14$; the time it takes to type the password a is $2$; the time it takes to type the password aaabacc is $11$.
You have to help Monocarp — insert a lowercase Latin letter into his password so that the resulting password takes the maximum possible amount of time to type.
The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases.
Each test case consists of one line containing the string $s$ ($1 \le |s| \le 10$), consisting of lowercase Latin letters.
For each test case, print one line containing the new password — a string which can be obtained from $s$ by inserting one lowercase Latin letter. The string you print should have the maximum possible required time to type it. If there are multiple answers, print any of them.
There is a grid, consisting of $2$ rows and $n$ columns. Each cell of the grid is either free or blocked.
A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions holds:
* $x$ and $y$ share a side; * there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachable from $z$.
A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.
For example, consider the following layout, where white cells are free, and dark grey cells are blocked:
![](CDN_BASE_URL/35b42e4e3c64eee3071df3d7b48861e8)
There are $3$ regions in it, denoted with red, green and blue color respectively:
![](CDN_BASE_URL/b2528153b76de41b1afcd49c1578a191)
The given grid contains at most $1$ connected region. Your task is to calculate the number of free cells meeting the following constraint:
* if this cell is blocked, the number of connected regions becomes exactly $3$.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of columns.
The $i$-th of the next two lines contains a description of the $i$-th row of the grid — the string $s_i$, consisting of $n$ characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell).
Additional constraint on the input:
* the given grid contains at most $1$ connected region; * the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
For each test case, print a single integer — the number of cells such that the number of connected regions becomes $3$ if this cell is blocked.
In the first test case, if the cell $(1, 3)$ is blocked, the number of connected regions becomes $3$ (as shown in the picture from the statement).
You are given a rooted tree, consisting of $n$ vertices. The vertices in the tree are numbered from $1$ to $n$, and the root is the vertex $1$. The value $a_i$ is written at the $i$-th vertex.
You can perform the following operation any number of times (possibly zero): choose a vertex $v$ which has at least one child; increase $a_v$ by $1$; and decrease $a_u$ by $1$ for all vertices $u$ that are in the subtree of $v$ (except $v$ itself). However, after each operation, the values on all vertices should be non-negative.
Your task is to calculate the maximum possible value written at the root using the aforementioned operation.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$) — the initial values written at vertices.
The third line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the parent of the $i$-th vertex in the tree. Vertex $1$ is the root.
Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
For each test case, print a single integer — the maximum possible value written at the root using the aforementioned operation.