rating
int64
800
3.5k
tags
listlengths
0
11
official_tests
listlengths
1
150
prompt
stringlengths
1.11k
5.74k
3,500
[ "binary search", "data structures", "trees" ]
[ { "input": "3\r\n3\r\n1 2\r\n2 3\r\n2 3\r\n6\r\n1 2\r\n1 3\r\n2 4\r\n2 5\r\n1 6\r\n4 5\r\n4\r\n1 2\r\n1 3\r\n2 4\r\n2 4\r\n", "output": "Bob\r\nAlice\r\nAlice\r\nBob\r\nAlice\r\nBob\r\nAlice\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 4.0 seconds Memory limit: 256.0 MB # Problem This is the hard version of the problem. In this version, it is not guaranteed that $$$u = v$$$. You can make hacks only if both versions of the problem are solved. Alice and Bob are playing a fun game on a tree. This game is played on a tree with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Recall that a tree with $$$n$$$ vertices is an undirected connected graph with $$$n - 1$$$ edges. Alice and Bob take turns, with Alice going first. Each player starts at some vertex. On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses. You are given two vertices $$$u$$$ and $$$v$$$. Represent the simple path from vertex $$$u$$$ to $$$v$$$ as an array $$$p_1, p_2, p_3, \ldots, p_m$$$, where $$$p_1 = u$$$, $$$p_m = v$$$, and there is an edge between $$$p_i$$$ and $$$p_{i + 1}$$$ for all $$$i$$$ ($$$1 \le i < m$$$). You need to determine the winner of the game if Alice starts at vertex $$$1$$$ and Bob starts at vertex $$$p_j$$$ for each $$$j$$$ (where $$$1 \le j \le m$$$). ## Input Format 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$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) — the number of vertices in the tree. Each of the following $$$n - 1$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n$$$), denoting an undirected edge between vertices $$$a$$$ and $$$b$$$. It is guaranteed that these edges form a tree. The last line of each test case contains two integers $$$u$$$ and $$$v$$$ ($$$2 \le u, v \le n$$$). It is guaranteed that the path from $$$u$$$ to $$$v$$$ does not pass through vertex $$$1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. ## Output Format For each test case, output $$$m$$$ lines. In the $$$i$$$-th line, print the winner of the game if Alice starts at vertex $$$1$$$ and Bob starts at vertex $$$p_i$$$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise. ## Examples ```input 3 3 1 2 2 3 2 3 6 1 2 1 3 2 4 2 5 1 6 4 5 4 1 2 1 3 2 4 2 4 ``` ```output Bob Alice Alice Bob Alice Bob Alice ``` ## Note Tree from the first example. In the first test case, the path will be ($$$2,3$$$). If Bob starts at vertex $$$2$$$, Alice will not be able to move anywhere on her first turn and will lose. However, if Bob starts at vertex $$$3$$$, Alice will move to vertex $$$2$$$, and Bob will have no remaining vertices to visit and will lose. Now solve the problem and return the code.
800
[ "brute force", "math" ]
[ { "input": "3\r\n2\r\n4\r\n6\r\n", "output": "1\r\n3\r\n5\r\n" }, { "input": "99\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67\r\n68\r\n69\r\n70\r\n71\r\n72\r\n73\r\n74\r\n75\r\n76\r\n77\r\n78\r\n79\r\n80\r\n81\r\n82\r\n83\r\n84\r\n85\r\n86\r\n87\r\n88\r\n89\r\n90\r\n91\r\n92\r\n93\r\n94\r\n95\r\n96\r\n97\r\n98\r\n99\r\n100\r\n", "output": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33\r\n34\r\n35\r\n36\r\n37\r\n38\r\n39\r\n40\r\n41\r\n42\r\n43\r\n44\r\n45\r\n46\r\n47\r\n48\r\n49\r\n50\r\n51\r\n52\r\n53\r\n54\r\n55\r\n56\r\n57\r\n58\r\n59\r\n60\r\n61\r\n62\r\n63\r\n64\r\n65\r\n66\r\n67\r\n68\r\n69\r\n70\r\n71\r\n72\r\n73\r\n74\r\n75\r\n76\r\n77\r\n78\r\n79\r\n80\r\n81\r\n82\r\n83\r\n84\r\n85\r\n86\r\n87\r\n88\r\n89\r\n90\r\n91\r\n92\r\n93\r\n94\r\n95\r\n96\r\n97\r\n98\r\n99\r\n" }, { "input": "99\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n100\r\n", "output": "99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n99\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 1.0 seconds Memory limit: 256.0 MB # Problem Cube is given an integer $$$n$$$. She wants to know how many ordered pairs of positive integers $$$(a,b)$$$ there are such that $$$a=n-b$$$. Since Cube is not very good at math, please help her! ## Input Format The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 99$$$) — the number of test cases. The only line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 100$$$). ## Output Format For each test case, output the number of ordered pairs $$$(a, b)$$$ on a new line. ## Examples ```input 3 2 4 6 ``` ```output 1 3 5 ``` ## Note In the first test case, the only ordered pair that works is $$$(a,b)=(1,1)$$$. In the second test case, the three ordered pairs of $$$(a,b)$$$ that work are $$$(3,1), (2,2), (1,3)$$$. Now solve the problem and return the code.
800
[ "implementation", "strings" ]
[ { "input": "5\r\nqwq\r\nppppp\r\npppwwwqqq\r\nwqpqwpqwwqp\r\npqpqpqpq\r\n", "output": "pwp\r\nqqqqq\r\npppwwwqqq\r\nqpwwpqwpqpw\r\npqpqpqpq\r\n" }, { "input": "1\r\npwwwqppppwppwwwpwwwppqpwwqppqpppwwwwpwwwwpwwwwqwqqwwww\r\n", "output": "wwwwppwpwwwwqwwwwqwwwwqqqpqqpwwqpqqwwwqwwwqqwqqqqpwwwq\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 1.0 seconds Memory limit: 256.0 MB # Problem A string consisting of only characters 'p', 'q', and 'w' is painted on a glass window of a store. Ship walks past the store, standing directly in front of the glass window, and observes string $$$a$$$. Ship then heads inside the store, looks directly at the same glass window, and observes string $$$b$$$. Ship gives you string $$$a$$$. Your job is to find and output $$$b$$$. ## Input Format The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases. The only line of each test case contains a string $$$a$$$ ($$$1 \leq |a| \leq 100$$$) — the string Ship observes from outside the store. It is guaranteed that $$$a$$$ only contains characters 'p', 'q', and 'w'. ## Output Format For each test case, output string $$$b$$$, the string Ship observes from inside the store, on a new line. ## Examples ```input 5 qwq ppppp pppwwwqqq wqpqwpqwwqp pqpqpqpq ``` ```output pwp qqqqq pppwwwqqq qpwwpqwpqpw pqpqpqpq ``` Now solve the problem and return the code.
1,300
[ "binary search", "greedy", "implementation", "math", "number theory" ]
[ { "input": "5\r\n2 2 6 2 12\r\n2 1 1000000000 1 1000000000\r\n3 5 7 15 63\r\n1000000000 1 5 6 1000000000\r\n15 17 78 2596 20914861\r\n", "output": "12\r\n1999999987\r\n6\r\n1\r\n197\r\n" }, { "input": "1\r\n53 644922653 836907643 264850321 782063425\r\n", "output": "137140773\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.0 seconds Memory limit: 256.0 MB # Problem Wave is given five integers $$$k$$$, $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, and $$$r_2$$$. Wave wants you to help her count the number of ordered pairs $$$(x, y)$$$ such that all of the following are satisfied: - $$$l_1 \leq x \leq r_1$$$. - $$$l_2 \leq y \leq r_2$$$. - There exists a non-negative integer $$$n$$$ such that $$$\frac{y}{x} = k^n$$$. ## Input Format The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The only line of each test case contains five integers $$$k$$$, $$$l_1$$$, $$$r_1$$$, $$$l_2$$$, and $$$r_2$$$ ($$$2 \leq k \leq 10^9, 1 \leq l_1 \leq r_1 \leq 10^9, 1 \leq l_2 \leq r_2 \leq 10^9$$$). ## Output Format For each test case, output the number of matching ordered pairs $$$(x, y)$$$ on a new line. ## Examples ```input 5 2 2 6 2 12 2 1 1000000000 1 1000000000 3 5 7 15 63 1000000000 1 5 6 1000000000 15 17 78 2596 20914861 ``` ```output 12 1999999987 6 1 197 ``` ## Note In the third test case, the matching ordered pairs are the following: - $$$(5,15)$$$ - $$$(5,45)$$$ - $$$(6,18)$$$ - $$$(6,54)$$$ - $$$(7,21)$$$ - $$$(7,63)$$$ In the fourth test case, the only valid ordered pair is $$$(1,1\,000\,000\,000)$$$ Now solve the problem and return the code.
1,900
[ "binary search", "brute force", "data structures", "math", "number theory" ]
[ { "input": "3 3 6\r\n-2 3 -3\r\n-2 2 -1\r\n-1\r\n1\r\n-2\r\n2\r\n-3\r\n3\r\n", "output": "NO\r\nYES\r\nNO\r\nNO\r\nYES\r\nNO\r\n" }, { "input": "5 5 6\r\n1 -2 3 0 0\r\n0 -2 5 0 -3\r\n4\r\n-3\r\n5\r\n2\r\n-1\r\n2\r\n", "output": "YES\r\nYES\r\nYES\r\nYES\r\nNO\r\nYES\r\n" }, { "input": "2 3 1\r\n-2 -2\r\n-3 2 -2\r\n2\r\n", "output": "YES\r\n" }, { "input": "1 15 6\r\n0\r\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\r\n-1\r\n1\r\n-2\r\n2\r\n-3\r\n3\r\n", "output": "NO\r\nNO\r\nNO\r\nNO\r\nNO\r\nNO\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 4.0 seconds Memory limit: 256.0 MB # Problem For an arbitrary grid, Robot defines its beauty to be the sum of elements in the grid. Robot gives you an array $$$a$$$ of length $$$n$$$ and an array $$$b$$$ of length $$$m$$$. You construct a $$$n$$$ by $$$m$$$ grid $$$M$$$ such that $$$M_{i,j}=a_i\cdot b_j$$$ for all $$$1 \leq i \leq n$$$ and $$$1 \leq j \leq m$$$. Then, Robot gives you $$$q$$$ queries, each consisting of a single integer $$$x$$$. For each query, determine whether or not it is possible to perform the following operation exactly once so that $$$M$$$ has a beauty of $$$x$$$: 1. Choose integers $$$r$$$ and $$$c$$$ such that $$$1 \leq r \leq n$$$ and $$$1 \leq c \leq m$$$ 2. Set $$$M_{i,j}$$$ to be $$$0$$$ for all ordered pairs $$$(i,j)$$$ such that $$$i=r$$$, $$$j=c$$$, or both. Note that queries are not persistent, meaning that you do not actually set any elements to $$$0$$$ in the process — you are only required to output if it is possible to find $$$r$$$ and $$$c$$$ such that if the above operation is performed, the beauty of the grid will be $$$x$$$. Also, note that you must perform the operation for each query, even if the beauty of the original grid is already $$$x$$$. ## Input Format The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1 \leq n,m \leq 2\cdot 10^5, 1 \leq q \leq 5\cdot 10^4$$$) — the length of $$$a$$$, the length of $$$b$$$, and the number of queries respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq |a_i| \leq n$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$0 \leq |b_i| \leq m$$$). The following $$$q$$$ lines each contain a single integer $$$x$$$ ($$$1 \leq |x| \leq 2\cdot 10^5$$$), the beauty of the grid you wish to achieve by setting all elements in a row and a column to $$$0$$$. ## Output Format For each testcase, output "YES" (without quotes) if there is a way to perform the aforementioned operation such that the beauty is $$$x$$$, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response). ## Examples ```input 3 3 6 -2 3 -3 -2 2 -1 -1 1 -2 2 -3 3 ``` ```output NO YES NO NO YES NO ``` ----- ```input 5 5 6 1 -2 3 0 0 0 -2 5 0 -3 4 -3 5 2 -1 2 ``` ```output YES YES YES YES NO YES ``` ## Note In the second example, the grid is 0 -2 5 0 -3 0 4 -10 0 6 0 -6 15 0 -9 0 0 0 0 0 0 0 0 0 0 By performing the operation with $$$r=4$$$ and $$$c=2$$$, we create the following grid: 0 0 5 0 -3 0 0 -10 0 6 0 0 15 0 -9 0 0 0 0 0 0 0 0 0 0 which has beauty $$$4$$$. Thus, we output YES. In the second query, selecting $$$r=3$$$ and $$$c=5$$$ creates a grid with beauty $$$-3$$$. In the third query, selecting $$$r=3$$$ and $$$c=3$$$ creates a grid with beauty $$$5$$$. Now solve the problem and return the code.
1,700
[ "dfs and similar", "graph matchings", "graphs", "implementation", "trees" ]
[ { "input": "5\r\n2\r\n2 1\r\n5\r\n2 3 4 5 1\r\n5\r\n2 1 4 2 3\r\n5\r\n4 1 1 5 4\r\n10\r\n4 3 9 1 6 7 9 10 10 3\r\n", "output": "2\r\n2\r\n5\r\n4\r\n5\r\n" }, { "input": "1\r\n6\r\n2 1 1 3 6 5\r\n", "output": "4\r\n" }, { "input": "1\r\n8\r\n2 1 4 5 6 2 6 7\r\n", "output": "6\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.0 seconds Memory limit: 256.0 MB # Problem This is the easy version of the problem. The key difference between the two versions is highlighted in bold. A group of $$$n$$$ spiders has come together to exchange plushies. Initially, each spider has $$$1$$$ plushie. Every year, if spider $$$i$$$ has at least one plushie, he will give exactly one plushie to spider $$$r_i$$$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, if any spider has more than $$$1$$$ plushie at any point in time, they will throw all but $$$1$$$ away. The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $$$1$$$ can never be stable. Find the first year in which the process becomes stable. ## Input Format The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of spiders. The following line contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$1 \leq r_i \leq n, r_i \neq i$$$) — the recipient of the plushie of each spider. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. ## Output Format For each test case, output an integer on a new line, the first year in which the process becomes stable. ## Examples ```input 5 2 2 1 5 2 3 4 5 1 5 2 1 4 2 3 5 4 1 1 5 4 10 4 3 9 1 6 7 9 10 10 3 ``` ```output 2 2 5 4 5 ``` ## Note For the second test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Since this array is the same as the previous year, this year is stable. For the third test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 0]$$$. Then, year $$$2$$$'s exchange happens. Note that even though two spiders gave spider $$$2$$$ plushies, spider $$$2$$$ may only keep one plushie. - At year $$$3$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 1, 0]$$$. Then, year $$$3$$$'s exchange happens. - At year $$$4$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 0, 0]$$$. Then, year $$$4$$$'s exchange happens. - At year $$$5$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 0, 0, 0]$$$. Since this array is the same as the previous year, this year is stable. Now solve the problem and return the code.
1,900
[ "dfs and similar", "dp", "dsu", "graphs", "implementation", "trees" ]
[ { "input": "5\r\n2\r\n2 1\r\n5\r\n2 3 4 5 1\r\n5\r\n2 1 4 2 3\r\n5\r\n4 1 1 5 4\r\n10\r\n4 3 9 1 6 7 9 10 10 3\r\n", "output": "2\r\n2\r\n5\r\n5\r\n5\r\n" }, { "input": "1\r\n6\r\n2 1 1 3 6 5\r\n", "output": "4\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.0 seconds Memory limit: 256.0 MB # Problem This is the hard version of the problem. The key difference between the two versions is highlighted in bold. A group of $$$n$$$ spiders has come together to exchange plushies. Initially, each spider has $$$1$$$ plushie. Every year, if spider $$$i$$$ has at least one plushie, he will give exactly one plushie to spider $$$r_i$$$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, each spider is allowed to have more than 1 plushie at any point in time. The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $$$1$$$ can never be stable. Find the first year in which the process becomes stable. ## Input Format The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^5$$$) — the number of spiders. The following line contains $$$n$$$ integers $$$r_1, r_2, \ldots, r_n$$$ ($$$1 \leq r_i \leq n, r_i \neq i$$$) — the recipient of the plushie of each spider. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. ## Output Format For each test case, output an integer on a new line, the first year in which the process becomes stable. ## Examples ```input 5 2 2 1 5 2 3 4 5 1 5 2 1 4 2 3 5 4 1 1 5 4 10 4 3 9 1 6 7 9 10 10 3 ``` ```output 2 2 5 5 5 ``` ## Note For the second test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Since this array is the same as the previous year, this year is stable. For the third test case: - At year $$$1$$$, the following array shows the number of plushies each spider has: $$$[1, 1, 1, 1, 1]$$$. Then, year $$$1$$$'s exchange happens. - At year $$$2$$$, the following array shows the number of plushies each spider has: $$$[1, 2, 1, 1, 0]$$$. Then, year $$$2$$$'s exchange happens. - At year $$$3$$$, the following array shows the number of plushies each spider has: $$$[1, 3, 0, 1, 0]$$$. Then, year $$$3$$$'s exchange happens. - At year $$$4$$$, the following array shows the number of plushies each spider has: $$$[1, 4, 0, 0, 0]$$$. Then, year $$$4$$$'s exchange happens. - At year $$$5$$$, the following array shows the number of plushies each spider has: $$$[1, 4, 0, 0, 0]$$$. Since this array is the same as the previous year, this year is stable. Now solve the problem and return the code.
2,100
[ "constructive algorithms", "data structures", "dp", "implementation", "math" ]
[ { "input": "2\r\n4 3\r\n1 5 2 4\r\n4 9 5 3\r\n4 5 2 3\r\n1 5 5 2\r\n1 1 4 4\r\n2 2 3 3\r\n1 2 4 3\r\n3 3\r\n1 2 3\r\n4 5 6\r\n7 8 9\r\n1 1 1 3\r\n1 3 3 3\r\n2 2 2 2\r\n", "output": "500 42 168 \r\n14 42 5 \r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 3.5 seconds Memory limit: 512.0 MB # Problem Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices. Swing is given an $$$n \times n$$$ matrix $$$M$$$ containing positive integers. He has $$$q$$$ queries to ask you. For each query, he gives you four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ and asks you to flatten the submatrix bounded by $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ into an array $$$A$$$. Formally, $$$A = [M_{(x1,y1)}, M_{(x1,y1+1)}, \ldots, M_{(x1,y2)}, M_{(x1+1,y1)}, M_{(x1+1,y1+1)}, \ldots, M_{(x2,y2)}]$$$. The following image depicts the flattening of a submatrix bounded by the red dotted lines. The orange arrows denote the direction that the elements of the submatrix are appended to the back of $$$A$$$, and $$$A$$$ is shown at the bottom of the image. Afterwards, he asks you for the value of $$$\sum_{i=1}^{|A|} A_i \cdot i$$$ (sum of $$$A_i \cdot i$$$ over all $$$i$$$). ## Input Format The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^3$$$) — the number of test cases. The first line of each test contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n \leq 2000, 1 \leq q \leq 10^6$$$) — the length of $$$M$$$ and the number of queries. The following $$$n$$$ lines contain $$$n$$$ integers each, the $$$i$$$'th of which contains $$$M_{(i,1)}, M_{(i,2)}, \ldots, M_{(i,n)}$$$ ($$$1 \leq M_{(i, j)} \leq 10^6$$$). The following $$$q$$$ lines contain four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, and $$$y_2$$$ ($$$1 \leq x_1 \leq x_2 \leq n, 1 \leq y_1 \leq y_2 \leq n$$$) — the bounds of the query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$10^6$$$. ## Output Format For each test case, output the results of the $$$q$$$ queries on a new line. ## Examples ```input 2 4 3 1 5 2 4 4 9 5 3 4 5 2 3 1 5 5 2 1 1 4 4 2 2 3 3 1 2 4 3 3 3 1 2 3 4 5 6 7 8 9 1 1 1 3 1 3 3 3 2 2 2 2 ``` ```output 500 42 168 14 42 5 ``` ## Note In the second query of the first test case, $$$A = [9, 5, 5, 2]$$$. Therefore, the sum is $$$1 \cdot 9 + 2 \cdot 5 + 3 \cdot 5 + 4 \cdot 2 = 42$$$. Now solve the problem and return the code.
900
[ "constructive algorithms", "geometry", "greedy", "math" ]
[ { "input": "4\r\n1 1 1\r\n3 4 1\r\n4 3 3\r\n3 4 4\r\n", "output": "0 0 1 1\r\n0 1 1 0\r\n0 0 3 3\r\n0 3 3 0\r\n0 0 3 3\r\n0 3 3 0\r\n0 0 3 3\r\n0 3 3 0\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.0 seconds Memory limit: 256.0 MB # Problem You are given a coordinate plane and three integers $$$X$$$, $$$Y$$$, and $$$K$$$. Find two line segments $$$AB$$$ and $$$CD$$$ such that 1. the coordinates of points $$$A$$$, $$$B$$$, $$$C$$$, and $$$D$$$ are integers; 2. $$$0 \le A_x, B_x, C_x, D_x \le X$$$ and $$$0 \le A_y, B_y, C_y, D_y \le Y$$$; 3. the length of segment $$$AB$$$ is at least $$$K$$$; 4. the length of segment $$$CD$$$ is at least $$$K$$$; 5. segments $$$AB$$$ and $$$CD$$$ are perpendicular: if you draw lines that contain $$$AB$$$ and $$$CD$$$, they will cross at a right angle. Note that it's not necessary for segments to intersect. Segments are perpendicular as long as the lines they induce are perpendicular. ## Input Format The first line contains a single integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases. Next, $$$t$$$ cases follow. The first and only line of each test case contains three integers $$$X$$$, $$$Y$$$, and $$$K$$$ ($$$1 \le X, Y \le 1000$$$; $$$1 \le K \le 1414$$$). Additional constraint on the input: the values of $$$X$$$, $$$Y$$$, and $$$K$$$ are chosen in such a way that the answer exists. ## Output Format For each test case, print two lines. The first line should contain $$$4$$$ integers $$$A_x$$$, $$$A_y$$$, $$$B_x$$$, and $$$B_y$$$ — the coordinates of the first segment. The second line should also contain $$$4$$$ integers $$$C_x$$$, $$$C_y$$$, $$$D_x$$$, and $$$D_y$$$ — the coordinates of the second segment. If there are multiple answers, print any of them. ## Examples ```input 4 1 1 1 3 4 1 4 3 3 3 4 4 ``` ```output 0 0 1 0 0 0 0 1 2 4 2 2 0 1 1 1 0 0 1 3 1 2 4 1 0 1 3 4 0 3 3 0 ``` ## Note The answer for the first test case is shown below: Now solve the problem and return the code.
1,300
[ "binary search", "brute force", "constructive algorithms", "greedy" ]
[ { "input": "4\r\n2\r\n1 2\r\n1\r\n7\r\n3\r\n2 4 9\r\n5\r\n1 5 8 10 13\r\n", "output": "1\r\n1\r\n2\r\n3\r\n" }, { "input": "1\r\n2\r\n1 100000000000000001\r\n", "output": "100000000000000000\r\n" }, { "input": "1\r\n5\r\n3 4 9 13 14\r\n", "output": "1\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.0 seconds Memory limit: 256.0 MB # Problem You are given a strip divided into cells, numbered from left to right from $$$0$$$ to $$$10^{18}$$$. Initially, all cells are white. You can perform the following operation: choose two white cells $$$i$$$ and $$$j$$$, such that $$$i \ne j$$$ and $$$|i - j| \le k$$$, and paint them black. A list $$$a$$$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $$$k$$$ for which this is possible. ## Input Format The first line contains a single integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 < a_i < 10^{18}$$$; $$$a_i < a_{i + 1}$$$). Additional constraint on the input: the sum of $$$n$$$ across all test cases does not exceed $$$2000$$$. ## Output Format For each test case, print a single integer — the minimum value of $$$k$$$ for which it is possible to paint all the given cells black. ## Examples ```input 4 2 1 2 1 7 3 2 4 9 5 1 5 8 10 13 ``` ```output 1 1 2 3 ``` ## Note In the first example, with $$$k=1$$$, it is possible to paint the cells $$$(1, 2)$$$. In the second example, with $$$k=1$$$, it is possible to paint the cells $$$(7, 8)$$$. In the third example, with $$$k=2$$$, it is possible to paint the cells $$$(2, 4)$$$ and $$$(8, 9)$$$. In the fourth example, with $$$k=3$$$, it is possible to paint the cells $$$(0, 1)$$$, $$$(5, 8)$$$ and $$$(10, 13)$$$. Now solve the problem and return the code.
1,500
[ "binary search", "brute force", "constructive algorithms", "data structures", "greedy", "implementation" ]
[ { "input": "4\r\n1\r\n1\r\n6\r\n101101\r\n7\r\n1110001\r\n5\r\n11111\r\n", "output": "1\r\n8\r\n18\r\n6\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.5 seconds Memory limit: 512.0 MB # Problem There is a shop that sells action figures near Monocarp's house. A new set of action figures will be released shortly; this set contains $$$n$$$ figures, the $$$i$$$-th figure costs $$$i$$$ coins and is available for purchase from day $$$i$$$ to day $$$n$$$. For each of the $$$n$$$ days, Monocarp knows whether he can visit the shop. Every time Monocarp visits the shop, he can buy any number of action figures which are sold in the shop (of course, he cannot buy an action figure that is not yet available for purchase). If Monocarp buys at least two figures during the same day, he gets a discount equal to the cost of the most expensive figure he buys (in other words, he gets the most expensive of the figures he buys for free). Monocarp wants to buy exactly one $$$1$$$-st figure, one $$$2$$$-nd figure, ..., one $$$n$$$-th figure from the set. He cannot buy the same figure twice. What is the minimum amount of money he has to spend? ## Input Format The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of two lines: - the first line contains one integer $$$n$$$ ($$$1 \le n \le 4 \cdot 10^5$$$) — the number of figures in the set (and the number of days); - the second line contains a string $$$s$$$ ($$$|s| = n$$$, each $$$s_i$$$ is either 0 or 1). If Monocarp can visit the shop on the $$$i$$$-th day, then $$$s_i$$$ is 1; otherwise, $$$s_i$$$ is 0. Additional constraints on the input: - in each test case, $$$s_n$$$ is 1, so Monocarp is always able to buy all figures during the $$$n$$$-th day; - the sum of $$$n$$$ over all test cases does not exceed $$$4 \cdot 10^5$$$. ## Output Format For each test case, print one integer — the minimum amount of money Monocarp has to spend. ## Examples ```input 4 1 1 6 101101 7 1110001 5 11111 ``` ```output 1 8 18 6 ``` ## Note In the first test case, Monocarp buys the $$$1$$$-st figure on the $$$1$$$-st day and spends $$$1$$$ coin. In the second test case, Monocarp can buy the $$$1$$$-st and the $$$3$$$-rd figure on the $$$3$$$-rd day, the $$$2$$$-nd and the $$$4$$$-th figure on the $$$4$$$-th day, and the $$$5$$$-th and the $$$6$$$-th figure on the $$$6$$$-th day. Then, he will spend $$$1+2+5=8$$$ coins. In the third test case, Monocarp can buy the $$$2$$$-nd and the $$$3$$$-rd figure on the $$$3$$$-rd day, and all other figures on the $$$7$$$-th day. Then, he will spend $$$1+2+4+5+6 = 18$$$ coins. Now solve the problem and return the code.
1,900
[ "binary search", "data structures", "dp", "implementation", "math" ]
[ { "input": "4\r\n1 2 5 10\r\n15\r\n1 1\r\n1 2\r\n1 3\r\n1 4\r\n1 5\r\n1 10\r\n5 10\r\n6 10\r\n2 8\r\n3 4\r\n3 10\r\n3 8\r\n5 6\r\n5 5\r\n1 8\r\n", "output": "1\r\n4\r\n12\r\n30\r\n32\r\n86\r\n56\r\n54\r\n60\r\n26\r\n82\r\n57\r\n9\r\n2\r\n61\r\n" }, { "input": "7\r\n1 4 5 6 7 8 9\r\n4\r\n1 11\r\n1 12\r\n1 15\r\n1 28\r\n", "output": "176\r\n206\r\n261\r\n490\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 4.0 seconds Memory limit: 512.0 MB # Problem You are given a sequence of integers $$$[a_1, a_2, \dots, a_n]$$$. Let $$$s(l,r)$$$ be the sum of elements from $$$a_l$$$ to $$$a_r$$$ (i. e. $$$s(l,r) = \sum\limits_{i=l}^{r} a_i$$$). Let's construct another sequence $$$b$$$ of size $$$\frac{n(n+1)}{2}$$$ as follows: $$$b = [s(1,1), s(1,2), \dots, s(1,n), s(2,2), s(2,3), \dots, s(2,n), s(3,3), \dots, s(n,n)]$$$. For example, if $$$a = [1, 2, 5, 10]$$$, then $$$b = [1, 3, 8, 18, 2, 7, 17, 5, 15, 10]$$$. You are given $$$q$$$ queries. During the $$$i$$$-th query, you are given two integers $$$l_i$$$ and $$$r_i$$$, and you have to calculate $$$\sum \limits_{j=l_i}^{r_i} b_j$$$. ## Input Format The first line contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10 \le a_i \le 10$$$). The third line contains one integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^5$$$). Then $$$q$$$ lines follow, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le \frac{n(n+1)}{2}$$$). ## Output Format Print $$$q$$$ integers, the $$$i$$$-th of which should be equal to $$$\sum \limits_{j=l_i}^{r_i} b_j$$$. ## Examples ```input 4 1 2 5 10 15 1 1 1 2 1 3 1 4 1 5 1 10 5 10 6 10 2 8 3 4 3 10 3 8 5 6 5 5 1 8 ``` ```output 1 4 12 30 32 86 56 54 60 26 82 57 9 2 61 ``` Now solve the problem and return the code.
2,700
[ "data structures", "dfs and similar", "divide and conquer", "dp", "implementation", "trees" ]
[ { "input": "12\r\n2 1 5 7\r\n2 1 3 4\r\n4 1 4\r\n4 1 8\r\n4 1 2\r\n1 1\r\n2 2 4 10\r\n4 1 9\r\n4 2 9\r\n3 1\r\n4 1 9\r\n4 2 9\r\n", "output": "4\r\n11\r\n0\r\n11\r\n17\r\n4\r\n17\r\n" }, { "input": "10\r\n4 1 3\r\n4 1 6\r\n2 1 2 6\r\n1 1\r\n4 1 8\r\n4 2 2\r\n4 1 9\r\n2 2 1 2\r\n2 2 2 6\r\n2 2 9 3\r\n", "output": "0\r\n0\r\n6\r\n6\r\n6\r\n" }, { "input": "20\r\n4 1 6\r\n4 1 19\r\n4 1 8\r\n4 1 5\r\n4 1 14\r\n4 1 9\r\n4 1 12\r\n4 1 4\r\n4 1 17\r\n4 1 16\r\n4 1 6\r\n4 1 16\r\n4 1 15\r\n4 1 11\r\n4 1 6\r\n4 1 3\r\n4 1 2\r\n4 1 10\r\n4 1 15\r\n4 1 18\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n" }, { "input": "50\r\n4 1 33\r\n4 1 16\r\n2 1 4 25\r\n4 1 44\r\n4 1 7\r\n2 1 47 19\r\n4 1 17\r\n2 1 46 23\r\n4 1 44\r\n2 1 4 28\r\n4 1 20\r\n4 1 49\r\n2 1 40 29\r\n1 1\r\n2 1 34 47\r\n4 1 9\r\n2 1 12 37\r\n4 1 46\r\n4 1 6\r\n2 1 27 43\r\n2 1 28 3\r\n4 2 28\r\n2 1 28 33\r\n2 1 41 35\r\n4 2 32\r\n2 1 28 3\r\n2 1 14 4\r\n4 2 24\r\n4 1 18\r\n4 1 22\r\n2 1 6 22\r\n1 2\r\n2 1 4 41\r\n2 1 13 36\r\n1 1\r\n2 1 23 34\r\n2 1 1 23\r\n4 3 48\r\n4 1 33\r\n1 1\r\n4 2 11\r\n4 4 10\r\n4 2 31\r\n4 1 46\r\n2 1 10 28\r\n4 5 39\r\n2 1 35 15\r\n4 1 14\r\n2 1 38 30\r\n2 1 34 11\r\n", "output": "0\r\n0\r\n25\r\n25\r\n25\r\n25\r\n53\r\n53\r\n53\r\n100\r\n28\r\n53\r\n53\r\n53\r\n65\r\n90\r\n82\r\n176\r\n53\r\n69\r\n53\r\n212\r\n190\r\n117\r\n" }, { "input": "1\r\n4 1 1\r\n", "output": "0\r\n" }, { "input": "10\r\n4 1 3\r\n4 1 6\r\n2 1 6 8\r\n3 1\r\n4 1 4\r\n4 1 1\r\n4 1 5\r\n2 1 7 9\r\n3 1\r\n2 1 1 2\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n" }, { "input": "20\r\n4 1 6\r\n4 1 19\r\n4 1 8\r\n4 1 5\r\n4 1 14\r\n4 1 9\r\n4 1 12\r\n4 1 4\r\n4 1 17\r\n4 1 16\r\n4 1 6\r\n4 1 16\r\n4 1 15\r\n4 1 11\r\n4 1 6\r\n4 1 3\r\n4 1 2\r\n4 1 10\r\n4 1 15\r\n4 1 18\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n" }, { "input": "50\r\n4 1 33\r\n4 1 16\r\n2 1 32 41\r\n4 1 7\r\n4 1 47\r\n3 1\r\n4 1 17\r\n2 1 47 20\r\n4 1 43\r\n2 1 46 23\r\n4 1 44\r\n4 1 48\r\n3 1\r\n3 1\r\n1 1\r\n4 1 28\r\n2 2 20 9\r\n4 1 43\r\n4 1 4\r\n2 2 40 29\r\n3 2\r\n4 1 26\r\n3 2\r\n1 2\r\n4 1 12\r\n2 2 12 37\r\n3 2\r\n4 2 6\r\n4 2 27\r\n4 3 43\r\n2 2 3 28\r\n3 2\r\n1 2\r\n2 4 12 30\r\n2 2 35 15\r\n3 2\r\n2 2 14 4\r\n4 4 24\r\n4 3 18\r\n3 2\r\n4 3 8\r\n4 2 15\r\n4 4 24\r\n4 2 25\r\n2 2 49 7\r\n4 4 29\r\n3 2\r\n4 3 18\r\n2 2 23 16\r\n3 2\r\n", "output": "0\r\n0\r\n0\r\n41\r\n0\r\n0\r\n0\r\n23\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n30\r\n0\r\n0\r\n0\r\n30\r\n0\r\n30\r\n0\r\n" }, { "input": "10\r\n4 1 3\r\n4 1 6\r\n2 1 6 8\r\n3 1\r\n4 1 4\r\n4 1 1\r\n4 1 5\r\n2 1 7 9\r\n3 1\r\n2 1 1 2\r\n", "output": "0\r\n0\r\n0\r\n0\r\n0\r\n" } ]
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in python3. Your solution must read input from standard input (cin), write output to standard output (cout). Do not include any debug prints or additional output. Put your final solution within a single code block: ```python <your code here> ``` Execution time limit: 2.0 seconds Memory limit: 1024.0 MB # Problem In the Bermart chain of stores, a variety of ice cream is sold. Each type of ice cream has two parameters: price and tastiness. Initially, there is one store numbered $$$1$$$, which sells nothing. You have to process $$$q$$$ queries of the following types: - $$$1~x$$$ — a new store opens, that sells the same types of ice cream as store $$$x$$$. It receives the minimum available positive index. The order of the types of ice cream in the new store is the same as in store $$$x$$$. - $$$2~x~p~t$$$ — a type of ice cream with price $$$p$$$ and tastiness $$$t$$$ becomes available in store $$$x$$$. - $$$3~x$$$ — a type of ice cream that was available the longest (appeared the earliest) in store $$$x$$$ is removed. - $$$4~x~p$$$ — for store $$$x$$$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $$$p$$$ (each type can be used in the subset no more than once). ## Input Format The first line contains a single integer $$$q$$$ ($$$1 \le q \le 3 \cdot 10^4$$$) — the number of queries. Each of the following $$$q$$$ lines contains a query in the format described in the statement: - $$$1~x$$$; - $$$2~x~p~t$$$ ($$$1 \le p, t \le 2000$$$); - $$$3~x$$$; - $$$4~x~p$$$ ($$$1 \le p \le 2000$$$). Additional constraints on the input data: - $$$x$$$ in each query does not exceed the current number of stores (that is, $$$1$$$ plus the number of type $$$1$$$ queries); - query type $$$3$$$ is not applied to a store that has no types of ice cream; - there is at least one query of type $$$4$$$. ## Output Format For each query of type $$$4$$$, output a single integer — for store $$$x$$$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $$$p$$$ (each type can be used in the subset no more than once). ## Examples ```input 12 2 1 5 7 2 1 3 4 4 1 4 4 1 8 4 1 2 1 1 2 2 4 10 4 1 9 4 2 9 3 1 4 1 9 4 2 9 ``` ```output 4 11 0 11 17 4 17 ``` Now solve the problem and return the code.