Search is not available for this dataset
question
stringlengths 29
14k
| solutions
stringlengths 2
1.9M
| starter_code
stringlengths 0
1.47k
| input_output
stringlengths 29
79.2M
| difficulty
stringclasses 6
values | raw_tags
stringlengths 2
281
| name
stringlengths 3
105
⌀ | source
stringclasses 10
values | tags
stringlengths 2
203
| skill_types
stringclasses 150
values | url
stringlengths 34
138
⌀ | Expected Auxiliary Space
stringclasses 335
values | time_limit
stringclasses 168
values | date
stringlengths 10
10
⌀ | picture_num
stringclasses 9
values | memory_limit
stringclasses 36
values | Expected Time Complexity
stringclasses 710
values |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
This is an interactive problem.
In good old times dwarves tried to develop extrasensory abilities:
* Exactly n dwarves entered completely dark cave.
* Each dwarf received a hat — white or black. While in cave, none of the dwarves was able to see either his own hat or hats of other Dwarves.
* Dwarves went out of the cave to the meadow and sat at an arbitrary place one after the other. When a dwarf leaves the cave, he sees the colors of all hats of all dwarves that are seating on the meadow (i.e. left the cave before him). However, he is not able to see the color of his own hat and none of the dwarves can give him this information.
* The task for dwarves was to got diverged into two parts — one with dwarves with white hats and one with black hats.
After many centuries, dwarves finally managed to select the right place on the meadow without error. Will you be able to repeat their success?
You are asked to successively name n different integer points on the plane. After naming each new point you will be given its color — black or white. Your task is to ensure that the named points can be split by a line in such a way that all points of one color lie on the same side from the line and points of different colors lie on different sides. Moreover, no points can belong to the line. Also, you need to report any such line at the end of the process.
In this problem, the interactor is adaptive — the colors of the points in the tests are not fixed beforehand and the jury program can select them arbitrarily, in particular, depending on your program output.
Interaction
The first line of the standard input stream contains an integer n (1 ≤ n ≤ 30) — the number of points your program should name.
Then n times your program must print two integer coordinates x and y (0 ≤ x ≤ 109, 0 ≤ y ≤ 109). All points you print must be distinct.
In response to each coordinate pair your program will receive the string "black", if the point is black, or "white", if the point is white.
When all n points are processed, you need to print four integers x1, y1, x2 and y2 (0 ≤ x1, y1 ≤ 109, 0 ≤ x2, y2 ≤ 109) — coordinates of points (x1, y1) and (x2, y2), which form a line, which separates n points into black and white. Points (x1, y1) and (x2, y2) should not coincide.
Hacks
To hack solution use the following format. The first line must contain word "hack", the second line should contain the number n and the last line should contain the sequence of 0 and 1 — colors of points, which will be reported to the solution. Unlike the jury tests, colors of points in hacks are always fixed in advance. Of course, the hacked solution wouldn't be able to get the information about the colors in advance.
For example, the hack corresponding to sample test will look like this:
hack
5
0 0 1 1 0
Example
Input
5
<span class="tex-span"></span>
black
<span class="tex-span"></span>
black
<span class="tex-span"></span>
white
<span class="tex-span"></span>
white
<span class="tex-span"></span>
black
Output
<span class="tex-span"></span>
0 0
<span class="tex-span"></span>
3 1
<span class="tex-span"></span>
2 3
<span class="tex-span"></span>
4 4
<span class="tex-span"></span>
0 2
<span class="tex-span"></span>
1 3 4 1
Note
In the sample input and output values are aligned only for simplicity of interpreting them chronologically. In real interaction no "extra" line breaks should appear.
The following picture illustrates the first test.
<image> | [] | {"inputs": ["hack\n30\n1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "random\n22\n2\n", "random\n20\n11\n", "random\n10\n1\n", "random\n20\n12\n", "random\n30\n14\n", "random\n23\n1\n", "predefined\n15\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n", "random\n21\n2\n", "random\n22\n1\n", "random\n28\n1\n", "random\n28\n2\n", "chess\n10\n", "chess\n11\n", "random\n25\n2\n", "random\n24\n1\n", "random\n13\n1\n", "chess\n19\n", "predefined\n15\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "random\n29\n1\n", "random\n20\n1\n", "random\n26\n1\n", "predefined\n2\n0 1\n", "random\n16\n1\n", "random\n15\n1\n", "random\n8\n1\n", "random\n19\n1\n", "random\n21\n1\n", "random\n27\n1\n", "predefined\n2\n0 0\n", "random\n2\n1\n", "predefined\n15\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "random\n3\n1\n", "predefined\n15\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n", "predefined\n29\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "hack\n30\n0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "predefined\n29\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n", "predefined\n30\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "random\n23\n2\n", "random\n29\n2\n", "random\n30\n13\n", "random\n9\n1\n", "predefined\n16\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n", "predefined\n8\n1 1 1 1 1 1 1 0\n", "random\n30\n2\n", "hack\n5\n0 0 1 1 0\n", "random\n25\n1\n", "random\n14\n1\n", "predefined\n8\n1 1 1 1 1 1 1 1\n", "random\n12\n1\n", "random\n30\n1\n", "random\n17\n1\n", "random\n26\n2\n", "chess\n30\n", "random\n18\n1\n", "predefined\n29\n0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n", "chess\n20\n", "predefined\n8\n0 0 0 0 0 0 0 0\n", "random\n27\n2\n", "random\n5\n1\n", "predefined\n29\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "random\n24\n2\n", "random\n11\n1\n", "random\n6\n1\n", "random\n4\n1\n", "predefined\n30\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "predefined\n1\n0\n", "predefined\n5\n0 1 0 1 1\n", "chess\n29\n", "random\n7\n1\n", "random\n1\n1\n", "predefined\n8\n0 1 0 1 0 1 0 1\n", "predefined\n1\n1\n", "hack\n30\n1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1\n", "random\n7\n2\n", "modnar\n20\n11\n", "random\n10\n2\n", "sandom\n20\n12\n", "random\n30\n25\n", "random\n34\n1\n", "oredefined\n15\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n", "radnom\n21\n2\n", "random\n21\n0\n", "rbndom\n28\n1\n", "random\n13\n2\n", "cgess\n10\n", "ciess\n11\n", "random\n2\n2\n", "random\n35\n1\n", "random\n13\n0\n", "shesc\n19\n", "predefined\n15\n1 1 2 1 1 1 1 1 1 1 1 1 1 1 1\n", "modnar\n29\n2\n", "modnar\n15\n1\n", "random\n26\n0\n", "predefinec\n2\n0 1\n", "random\n8\n2\n", "random\n15\n0\n", "rnadom\n8\n1\n", "rbndom\n19\n1\n", "sandom\n21\n1\n", "r`ndom\n27\n1\n", "denifederp\n2\n0 1\n", "rbndom\n3\n1\n", "predefined\n15\n0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0\n", "random\n3\n0\n", "predefined\n15\n0 1 0 1 0 1 0 1 0 0 0 1 0 1 0\n", "predefined\n3\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "hack\n30\n0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\n", "predefined\n29\n1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0\n", "predefined\n30\n0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\n", "random\n15\n2\n", "random\n58\n2\n", "random\n30\n0\n", "raodom\n9\n1\n", "predefined\n16\n1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0\n", "predefined\n8\n0 1 1 1 1 1 1 0\n", "r`ndom\n30\n2\n", "hack\n5\n0 1 1 1 0\n", "modnar\n2\n1\n", "random\n14\n2\n", "precefined\n8\n1 1 1 1 1 1 1 1\n", "r`ndom\n14\n1\n", "random\n30\n4\n", "raneom\n17\n1\n", "randon\n26\n2\n", "ssehc\n30\n", "random\n31\n1\n", "predefined\n29\n0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n", "chess\n13\n", "denifederp\n8\n0 0 0 0 0 0 0 0\n", "randpm\n27\n2\n", "random\n5\n2\n", "5\n<span class=\"tex-span\"></span>\nblack\n<span class=\"tex-span\"></span>\nblack\n<span class=\"tex-span\"></span>\nwhite\n<span class=\"tex-span\"></span>\nwhite\n<span class=\"tex-span\"></span>\nblack\n"], "outputs": ["0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n0 0 1000000000 2\n", "0 1\n500000000 1\n250000000 1\n125000000 1\n187500000 1\n125000000 0 187500000 2\n"]} | HARD | ['interactive', 'binary search', 'geometry', 'constructive algorithms'] | null | codeforces | ['Geometry', 'Sorting', 'Constructive algorithms'] | ['Sorting'] | https://codeforces.com/problemset/problem/1063/C | null | 2.0 seconds | null | null | 256.0 megabytes | null |
|
There are $n$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $1$ to $n$. The $i$-th box contains $r_i$ candies, candies have the color $c_i$ (the color can take one of three values — red, green, or blue). All candies inside a single box have the same color (and it is equal to $c_i$).
Initially, Tanya is next to the box number $s$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second.
If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled.
It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one.
Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies.
Tanya wants to eat at least $k$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements.
-----Input-----
The first line contains three integers $n$, $s$ and $k$ ($1 \le n \le 50$, $1 \le s \le n$, $1 \le k \le 2000$) — number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $n$ integers $r_i$ ($1 \le r_i \le 50$) — numbers of candies in the boxes. The third line contains sequence of $n$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces.
-----Output-----
Print minimal number of seconds to eat at least $k$ candies. If solution doesn't exist, print "-1".
-----Examples-----
Input
5 3 10
1 2 3 4 5
RGBRR
Output
4
Input
2 1 15
5 6
RG
Output
-1
-----Note-----
The sequence of actions of Tanya for the first example:
move from the box $3$ to the box $2$; eat candies from the box $2$; move from the box $2$ to the box $3$; eat candy from the box $3$; move from the box $3$ to the box $4$; move from the box $4$ to the box $5$; eat candies from the box $5$.
Since Tanya eats candy instantly, the required time is four seconds. | ["INF = 10000000000.0\nmax_n = 50\nmax_k = 2000\n\ndef main():\n\t(n, s, k) = map(int, input().split())\n\ts -= 1\n\tbuf = [''] * (max_n + 1)\n\tdp = [[0 for i in range(max_n + 1)] for j in range(max_k + 1)]\n\tr = list(map(int, input().split()))\n\tc = input()\n\tanswer = INF\n\tfor i in range(len(c)):\n\t\tbuf[i] = c[i]\n\tfor i in range(k, -1, -1):\n\t\tfor j in range(n):\n\t\t\tdp[i][j] = INF\n\tfor j in range(n):\n\t\tvalue = abs(j - s)\n\t\tif k - r[j] <= 0:\n\t\t\tanswer = min(answer, value)\n\t\telse:\n\t\t\tdp[k - r[j]][j] = value\n\tfor i in range(k, 0, -1):\n\t\tfor j in range(n):\n\t\t\tif dp[i][j] < INF:\n\t\t\t\tfor l in range(n):\n\t\t\t\t\tif buf[j] != buf[l] and r[j] < r[l]:\n\t\t\t\t\t\tvalue = dp[i][j] + abs(j - l)\n\t\t\t\t\t\tif i - r[l] <= 0:\n\t\t\t\t\t\t\tanswer = min(answer, value)\n\t\t\t\t\t\telse:\n\t\t\t\t\t\t\tdp[i - r[l]][l] = min(dp[i - r[l]][l], value)\n\tif answer == INF:\n\t\tprint(-1)\n\t\treturn\n\tprint(answer)\n\ndef __starting_point():\n\tmain()\n__starting_point()\n", "(n, s, k) = map(int, input().split())\ns -= 1\nr = list(map(int, input().split()))\nINF = float('inf')\nc = input()\ndp = [[] for i in range(n)]\n\ndef calc(u):\n\tif dp[u]:\n\t\treturn\n\tdp[u] = [0] * (r[u] + 1) + [INF] * (k - r[u])\n\tfor i in range(n):\n\t\tif c[u] != c[i] and r[i] > r[u]:\n\t\t\tcalc(i)\n\t\t\td = abs(u - i)\n\t\t\tfor j in range(r[u] + 1, k + 1):\n\t\t\t\tdp[u][j] = min(dp[u][j], dp[i][j - r[u]] + d)\nans = INF\nfor i in range(n):\n\tcalc(i)\n\tans = min(ans, abs(i - s) + dp[i][k])\nif ans == INF:\n\tprint(-1)\nelse:\n\tprint(ans)\n", "import math\n\ndef solve():\n\t(n, s, k) = map(int, input().split())\n\ts -= 1\n\tr = list(map(int, input().split()))\n\tc = input()\n\tinf = int(1000000000.0)\n\tdp = [[inf for j in range(n)] for i in range(k + 1)]\n\tfor i in range(0, k + 1):\n\t\tfor j in range(0, n):\n\t\t\tif i == 0 or i <= r[j]:\n\t\t\t\tdp[i][j] = 0\n\t\t\t\tcontinue\n\t\t\tfor K in range(0, n):\n\t\t\t\tif c[K] != c[j] and r[K] > r[j]:\n\t\t\t\t\tdp[i][j] = min(dp[i][j], dp[i - r[j]][K] + int(abs(K - j)))\n\tans = min((dp[k][i] + abs(i - s) for i in range(0, n)))\n\tif ans >= inf:\n\t\tprint(-1)\n\t\treturn\n\tprint(ans)\n\treturn\nt = 1\nwhile t > 0:\n\tt -= 1\n\tsolve()\n", "INF = 100000\n(n, s, k) = list(map(int, input().split()))\nr = list(map(int, input().split()))\nc = input().rstrip()\ndp = [[INF for j in range(k + 1)] for i in range(n)]\ns -= 1\nfor i in range(n):\n\tdp[i][k - r[i]] = abs(s - i)\nfor j in range(k, -1, -1):\n\tfor i in range(n):\n\t\tif dp[i][j] >= INF:\n\t\t\tcontinue\n\t\tfor f in range(n):\n\t\t\tif r[f] <= r[i]:\n\t\t\t\tcontinue\n\t\t\tif c[f] == c[i]:\n\t\t\t\tcontinue\n\t\t\tnew_val = max(0, j - r[f])\n\t\t\tdp[f][new_val] = min(dp[f][new_val], dp[i][j] + abs(i - f))\nans = INF\nfor i in range(n):\n\tans = min(ans, dp[i][0])\nif ans >= INF:\n\tans = -1\nprint(ans)\n", "(n, s, k) = map(int, input().split())\nr = list(map(int, input().split()))\ns -= 1\nc = input()\nbest = [[0 for i in range(n)] for j in range(k + 1)]\nfor i in range(1, k + 1):\n\tfor j in range(n):\n\t\tif i <= r[j]:\n\t\t\tbest[i][j] = abs(j - s)\n\t\telse:\n\t\t\tgood = float('inf')\n\t\t\tfor l in range(n):\n\t\t\t\tif c[j] != c[l] and r[j] > r[l]:\n\t\t\t\t\tgood = min(good, best[i - r[j]][l] + abs(j - l))\n\t\t\tbest[i][j] = good\nif min(best[-1]) == float('inf'):\n\tprint(-1)\nelse:\n\tprint(min(best[-1]))\n", "import sys\nsys.setrecursionlimit(1000)\n\ndef rec(r, c, s, K, k, dp):\n\tif (k, s) in dp:\n\t\treturn dp[k, s]\n\tif k <= 0:\n\t\treturn 0\n\tn = len(r)\n\tbesttime = 10 ** 10\n\tfor i in range(n):\n\t\tif r[i] > r[s] and c[i] != c[s] or k == K:\n\t\t\ttimetakenbelow = rec(r, c, i, K, k - r[i], dp)\n\t\t\ttimetaken = timetakenbelow + abs(s - i)\n\t\t\tif timetaken < besttime:\n\t\t\t\tbesttime = timetaken\n\tdp[k, s] = besttime\n\treturn besttime\n\ndef answer(n, s, K, r, c):\n\tdp = dict()\n\tk = K\n\tans = rec(r, c, s, K, k, dp)\n\tif ans == 10 ** 10:\n\t\treturn -1\n\treturn ans\n\ndef main():\n\t(n, s, K) = map(int, sys.stdin.readline().split())\n\tr = tuple(map(int, sys.stdin.readline().split()))\n\tc = sys.stdin.readline().rstrip()\n\tprint(answer(n, s - 1, K, r, c))\n\treturn\nmain()\n", "INF = 10000000000.0\n(n, s, k) = map(int, input().split())\nr = list(map(int, input().split()))\nr.append(0)\ncol = input()\nmat = []\nfor i in range(n + 1):\n\tadj = {}\n\tfor j in range(n):\n\t\tif i == n:\n\t\t\tadj[j] = abs(s - 1 - j)\n\t\telif col[i] != col[j] and r[i] < r[j]:\n\t\t\tadj[j] = abs(i - j)\n\tmat.append(adj)\nmem = [{} for i in range(n + 1)]\n\ndef get(s, k):\n\tif mem[s].get(k):\n\t\treturn mem[s].get(k)\n\tif r[s] >= k:\n\t\tmem[s][k] = 0\n\telse:\n\t\tmi = None\n\t\tfor nei in mat[s]:\n\t\t\tncost = get(nei, k - r[s])\n\t\t\tif ncost is None:\n\t\t\t\tcontinue\n\t\t\tcurr = ncost + mat[s][nei]\n\t\t\tif mi is None or curr < mi:\n\t\t\t\tmi = curr\n\t\tif mi is not None:\n\t\t\tmem[s][k] = mi\n\t\telse:\n\t\t\tmem[s][k] = INF\n\treturn mem[s].get(k)\nans = get(n, k)\nif ans is None or ans >= INF:\n\tprint(-1)\nelse:\n\tprint(ans)\n", "import sys\n\ndef minp():\n\treturn sys.stdin.readline().strip()\ndp = [None] * 50\nfor j in range(50):\n\tdp[j] = [None] * 2001\n(n, s, k) = map(int, minp().split())\na = [None] * n\ni = 0\ns -= 1\nfor j in map(int, minp().split()):\n\ta[i] = (j, i)\n\ti += 1\ni = 0\nfor j in minp():\n\ta[i] += ('RGB'.find(j),)\n\ti += 1\na.sort()\nr = 10 ** 18\nzzz = 0\nfor i in range(n):\n\tii = dp[i]\n\tc = a[i][0]\n\tii[c] = abs(s - a[i][1])\n\tfor j in range(i):\n\t\tif a[j][2] == a[i][2] or a[j][0] == a[i][0]:\n\t\t\tcontinue\n\t\tjj = dp[j]\n\t\tfor z in range(2001 - c):\n\t\t\tzz = jj[z]\n\t\t\tif zz != None:\n\t\t\t\td = zz + abs(a[i][1] - a[j][1])\n\t\t\t\tcc = z + c\n\t\t\t\tif ii[cc] != None:\n\t\t\t\t\tif ii[cc] > d:\n\t\t\t\t\t\tii[cc] = d\n\t\t\t\telse:\n\t\t\t\t\tii[cc] = d\n\tfor z in range(k, 2001):\n\t\tif ii[z] != None:\n\t\t\tr = min(r, ii[z])\nif r != 10 ** 18:\n\tprint(r)\nelse:\n\tprint(-1)\n", "inf = 10000\n(n, s, k) = map(int, input().split())\na = list(map(int, input().split()))\nb = list(input())\nfor i in range(n):\n\tif b[i] == 'R':\n\t\tb[i] = 0\n\telif b[i] == 'G':\n\t\tb[i] = 1\n\telse:\n\t\tb[i] = 2\nboxes = [[a[i], b[i], i] for i in range(n)]\nboxes.sort()\nl = boxes[-1][0] * n + 1\ns -= 1\ndp = [[[inf, s, -1] for j in range(l)] for i in range(3)]\nif l < k:\n\tprint(-1)\n\treturn\ndp[0][0][0] = 0\ndp[1][0][0] = 0\ndp[2][0][0] = 0\nfor i in range(n):\n\tpos = boxes[i][2]\n\tclr = boxes[i][1]\n\tcnt = boxes[i][0]\n\tfor j in range(l - cnt):\n\t\tfor c in range(3):\n\t\t\tif c == clr:\n\t\t\t\tcontinue\n\t\t\tif dp[clr][j + cnt][0] > dp[c][j][0] + abs(dp[c][j][1] - pos) and cnt > dp[c][j][2]:\n\t\t\t\tdp[clr][j + cnt][0] = dp[c][j][0] + abs(dp[c][j][1] - pos)\n\t\t\t\tdp[clr][j + cnt][1] = pos\n\t\t\t\tdp[clr][j + cnt][2] = cnt\nans = min(dp[0][k][0], min(dp[1][k][0], dp[2][k][0]))\nfor i in range(k, l):\n\tans = min(min(ans, dp[0][i][0]), min(dp[1][i][0], dp[2][i][0]))\nif ans < inf:\n\tprint(ans)\nelse:\n\tprint(-1)\n", "(n, s, k) = list(map(int, input().split()))\namounts = list(map(int, input().split()))\ncolors = list(input())\ndp = [[-1 for j in range(k + 1)] for i in range(n)]\n\ndef getAns(nth, left):\n\tif left <= 0:\n\t\treturn 0\n\tif dp[nth][left] >= 0:\n\t\treturn dp[nth][left]\n\tret = 999999999\n\tfor i in range(n):\n\t\tif amounts[i] <= amounts[nth] or colors[i] == colors[nth]:\n\t\t\tcontinue\n\t\tret = min(ret, abs(nth - i) + getAns(i, left - amounts[i]))\n\tdp[nth][left] = ret\n\treturn ret\nans = 999999999\nfor i in range(n):\n\tans = min(ans, getAns(i, k - amounts[i]) + abs(s - 1 - i))\nif ans == 999999999:\n\tans = -1\nprint(ans)\n"] | {"inputs": ["5 3 10\n1 2 3 4 5\nRGBRR\n", "2 1 15\n5 6\nRG\n", "6 1 21\n4 2 3 5 1 6\nRGBGRB\n", "6 1 21\n6 5 4 3 2 1\nRGBRGB\n", "1 1 10\n10\nR\n", "2 1 10\n5 5\nRG\n", "2 1 10\n5 6\nRR\n", "5 3 10\n1 2 3 4 5\nRGBRG\n", "9 1 6\n1 1 1 3 3 3 2 2 2\nRGGBRRGBB\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 10 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 33 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "50 32 600\n21 21 18 47 16 11 10 46 9 15 27 5 11 42 29 25 16 41 31 8 12 28 1 24 17 40 45 12 33 32 34 2 45 17 49 17 20 42 15 17 8 29 2 20 4 27 50 1 49 1\nBBRBBGBGBBRBGRRGRGGGBGBRRBBBGGBBBBGBGBRBBGRRGGBRGR\n", "50 37 500\n25 43 15 16 29 23 46 18 15 21 33 26 38 25 2 17 48 50 33 31 3 45 40 12 42 29 37 42 7 11 47 16 44 17 27 46 32 23 14 7 27 25 13 32 43 33 36 39 35 7\nGGBBRGBRRRRBBRGBRRRGGRGGRGGBRRRGBBRRGRGGRBGBGGRGBR\n", "50 4 200\n14 10 50 47 41 9 22 21 42 36 50 10 27 28 39 1 36 12 45 35 17 3 15 25 32 4 34 39 44 34 20 15 18 1 38 25 20 45 24 9 18 15 35 36 12 9 28 4 44 10\nBGBRRBGBRRRGRGRBRGGGRBRRGBBGGRBRRGGRGGGBRRBRGGBGBG\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "30 28 208\n3 42 42 47 46 44 5 28 35 28 35 44 25 44 47 3 3 35 28 5 3 42 3 46 25 25 5 47 46 3\nBGBBGBBBBGRRGGGBRGRGRRGBBRRRRG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 20 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 3 17 12 4 28 25 14 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "48 25 323\n39 37 32 4 4 32 18 44 49 4 12 12 12 22 22 37 38 32 24 45 44 37 18 39 45 22 24 22 45 39 4 22 24 22 12 49 4 29 18 38 29 29 38 44 12 12 49 4\nRRRRRBRRGBBRGRGGBGGBGBBBRBRGGGGBBRGRBGGGRBRBBRBG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 37 49 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "50 50 2000\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "30 28 208\n3 42 42 47 46 44 5 28 35 28 35 44 25 44 47 3 3 35 28 5 3 42 3 46 25 25 5 47 46 3\nBGBBGBBBBGRRGGGBRGRGRRGBBRRRRG\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 10 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "50 32 600\n21 21 18 47 16 11 10 46 9 15 27 5 11 42 29 25 16 41 31 8 12 28 1 24 17 40 45 12 33 32 34 2 45 17 49 17 20 42 15 17 8 29 2 20 4 27 50 1 49 1\nBBRBBGBGBBRBGRRGRGGGBGBRRBBBGGBBBBGBGBRBBGRRGGBRGR\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 3 17 12 4 28 25 14 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "1 1 10\n10\nR\n", "9 1 6\n1 1 1 3 3 3 2 2 2\nRGGBRRGBB\n", "5 3 10\n1 2 3 4 5\nRGBRG\n", "6 1 21\n6 5 4 3 2 1\nRGBRGB\n", "2 1 10\n5 5\nRG\n", "2 1 10\n5 6\nRR\n", "50 50 2000\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 37 49 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "48 25 323\n39 37 32 4 4 32 18 44 49 4 12 12 12 22 22 37 38 32 24 45 44 37 18 39 45 22 24 22 45 39 4 22 24 22 12 49 4 29 18 38 29 29 38 44 12 12 49 4\nRRRRRBRRGBBRGRGGBGGBGBBBRBRGGGGBBRGRBGGGRBRBBRBG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 20 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 33 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "50 4 200\n14 10 50 47 41 9 22 21 42 36 50 10 27 28 39 1 36 12 45 35 17 3 15 25 32 4 34 39 44 34 20 15 18 1 38 25 20 45 24 9 18 15 35 36 12 9 28 4 44 10\nBGBRRBGBRRRGRGRBRGGGRBRRGBBGGRBRRGGRGGGBRRBRGGBGBG\n", "6 1 21\n4 2 3 5 1 6\nRGBGRB\n", "50 37 500\n25 43 15 16 29 23 46 18 15 21 33 26 38 25 2 17 48 50 33 31 3 45 40 12 42 29 37 42 7 11 47 16 44 17 27 46 32 23 14 7 27 25 13 32 43 33 36 39 35 7\nGGBBRGBRRRRBBRGBRRRGGRGGRGGBRRRGBBRRGRGGRBGBGGRGBR\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 1 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 3 17 12 4 28 25 21 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "9 1 6\n1 1 2 3 3 3 2 2 2\nRGGBRRGBB\n", "2 1 10\n9 5\nRG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 37 2 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 10 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 37 500\n25 43 15 16 29 23 46 18 15 21 33 26 38 25 2 17 48 50 33 31 3 23 40 12 42 29 37 42 7 11 47 16 44 17 27 46 32 23 14 7 27 25 13 32 43 33 36 39 35 7\nGGBBRGBRRRRBBRGBRRRGGRGGRGGBRRRGBBRRGRGGRBGBGGRGBR\n", "39 36 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 10 29 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "30 28 208\n3 42 42 47 46 12 5 28 35 28 35 44 25 44 47 3 3 35 28 5 3 42 3 46 25 25 5 47 46 3\nBGBBGBBBBGRRGGGBRGRGRRGBBRRRRG\n", "9 1 6\n1 1 1 3 3 6 2 2 2\nRGGBRRGBB\n", "5 3 10\n1 2 4 4 5\nRGBRG\n", "39 21 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 20 44 21 34 36 39 30 34 21 20 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 4 200\n14 10 50 47 41 9 22 21 42 36 50 10 27 28 39 1 36 12 45 35 17 3 15 25 32 4 34 39 44 34 20 15 18 1 38 25 20 3 24 9 18 15 35 36 12 9 28 4 44 10\nBGBRRBGBRRRGRGRBRGGGRBRRGBBGGRBRRGGRGGGBRRBRGGBGBG\n", "6 1 21\n6 5 4 3 4 1\nRGBRGB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 23 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n4 2 3 5 1 6\nRGBRGB\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "2 1 24\n5 6\nRG\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 27 5 46 12 6 46 3 17 12 4 28 25 21 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "9 1 6\n1 1 2 5 3 3 2 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 23 15 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n4 2 3 5 1 6\nBGRBGR\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 44 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "9 1 6\n1 1 1 5 3 3 2 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "39 36 282\n13 39 20 29 30 14 29 29 30 29 16 39 50 13 16 45 36 36 13 10 29 21 34 36 39 30 34 21 21 14 16 45 21 45 29 34 50 50 14\nGGGBRRGRBGBRRBRGRBRBBGBGBGRRRGGRBBRGBGB\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 29 49 23 15 14 25 46 43 7 47 28 50 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n5 2 3 5 1 6\nBGRBGR\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 44 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGGGGGGGGGGGGGGGGGGGGGGGGGRRRRRRRRRRRRRRRRRRRRRRRRR\n", "9 1 6\n1 1 1 5 3 1 2 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 5 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 24 17 23 48 20 44 46 44 13 4 40 49 23 15 14 25 46 43 7 47 28 50 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 17 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 11 44 42 40 38 36 34 32 44 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGGGGGGGGGGGGGGGGGGGGGGGGGRRRRRRRRRRRRRRRRRRRRRRRRR\n", "9 1 6\n1 1 1 5 3 1 4 2 2\nRGGBRRGBB\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 10 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 10 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 10 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 50 2000\n1 3 7 7 9 11 13 15 32 19 21 32 25 10 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 46 36 34 32 30 28 26 24 22 20 18 16 14 12 10 1 10 6 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "50 39 2000\n48 43 26 24 46 37 15 30 39 34 4 14 29 34 8 18 40 8 17 40 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 10 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "48 2 259\n25 31 22 30 30 17 31 50 28 30 46 43 4 6 10 22 50 14 5 46 12 6 46 4 17 12 4 28 25 14 5 5 6 14 22 12 17 43 43 10 4 3 31 3 25 28 50 10\nBBBBGGRRBRRBBRGGGBGGRGBRBGRGRGRBBRRBRRGBGBGGGRBR\n", "1 1 20\n10\nR\n", "2 1 16\n5 5\nRG\n", "2 1 10\n1 6\nRR\n", "50 50 2000\n1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 7 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nGRGRGBBGGRGGRRRGGBGGGRRRBGRRBGBRGBBGGGGRRGGBBRRRRG\n", "48 33 357\n18 37 22 21 4 17 39 32 40 43 29 29 50 21 39 43 11 11 4 50 36 40 32 50 18 32 11 36 29 36 22 21 29 43 49 18 17 29 37 40 17 43 49 4 39 49 22 29\nGRGGGGBRBRRGGRGBRGBBGRBRRGBBRRBBBGRBBBBGRGGRRBRG\n", "50 49 1000\n30 37 34 31 26 44 32 12 36 15 5 5 31 24 17 24 43 19 17 23 45 2 31 17 23 48 20 44 46 44 13 4 29 49 33 41 14 25 46 43 7 47 28 25 2 30 37 37 19 32\nGBBBRBGRBRBRGRGRBBGBGRRBGGRBGRBRRRRRRRBRGRGGGGBRGG\n", "6 1 21\n4 2 3 2 1 6\nRGBGRB\n", "50 50 1250\n1 3 5 7 9 11 13 15 17 19 21 1 25 27 29 31 33 35 37 39 41 43 45 47 49 50 48 46 44 42 40 38 36 34 32 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2\nRRRRRRRRRRRRRRRRRRRRRRRRRGGGGGGGGGGGGGGGGGGGGGGGGG\n", "2 1 15\n5 1\nRG\n", "50 39 2000\n48 43 26 24 46 37 15 30 36 34 4 14 29 34 8 18 40 8 17 37 15 29 2 23 41 7 12 13 36 11 24 22 26 46 11 31 1 46 11 35 6 41 16 50 11 1 46 20 46 28\nBGBBBBBBRGGBBBRRRRBBGRGGRBBRBBBRBBBBBRRGBGGRRRBBRB\n", "2 1 15\n5 6\nRG\n", "5 3 10\n1 2 3 4 5\nRGBRR\n"], "outputs": ["4\n", "-1\n", "15\n", "10\n", "0\n", "-1\n", "-1\n", "2\n", "7\n", "-1\n", "-1\n", "185\n", "86\n", "23\n", "992\n", "20\n", "24\n", "39\n", "64\n", "63\n", "-1\n", "20\n", "-1\n", "185\n", "39\n", "0\n", "7\n", "2\n", "10\n", "-1\n", "-1\n", "-1\n", "63\n", "64\n", "24\n", "-1\n", "23\n", "15\n", "86\n", "992", "-1\n", "39\n", "3\n", "2\n", "63\n", "24\n", "86\n", "31\n", "20\n", "5\n", "4\n", "28\n", "23\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "39\n", "3\n", "-1\n", "-1\n", "-1\n", "-1\n", "3\n", "-1\n", "31\n", "-1\n", "-1\n", "-1\n", "3\n", "-1\n", "-1\n", "-1\n", "3\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "39\n", "-1\n", "-1\n", "-1\n", "-1\n", "63\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "-1\n", "4\n"]} | HARD | ['dp'] | null | codeforces | ['Dynamic programming'] | ['Dynamic programming'] | https://codeforces.com/problemset/problem/1057/C | null | null | 2019-12-31 | null | null | null |
|
Little Petya likes to play a lot. Most of all he likes to play a game «Holes». This is a game for one person with following rules:
There are N holes located in a single row and numbered from left to right with numbers from 1 to N. Each hole has it's own power (hole number i has the power ai). If you throw a ball into hole i it will immediately jump to hole i + ai, then it will jump out of it and so on. If there is no hole with such number, the ball will just jump out of the row. On each of the M moves the player can perform one of two actions:
* Set the power of the hole a to value b.
* Throw a ball into the hole a and count the number of jumps of a ball before it jump out of the row and also write down the number of the hole from which it jumped out just before leaving the row.
Petya is not good at math, so, as you have already guessed, you are to perform all computations.
Input
The first line contains two integers N and M (1 ≤ N ≤ 105, 1 ≤ M ≤ 105) — the number of holes in a row and the number of moves. The second line contains N positive integers not exceeding N — initial values of holes power. The following M lines describe moves made by Petya. Each of these line can be one of the two types:
* 0 a b
* 1 a
Type 0 means that it is required to set the power of hole a to b, and type 1 means that it is required to throw a ball into the a-th hole. Numbers a and b are positive integers do not exceeding N.
Output
For each move of the type 1 output two space-separated numbers on a separate line — the number of the last hole the ball visited before leaving the row and the number of jumps it made.
Examples
Input
8 5
1 1 1 1 1 2 8 2
1 1
0 1 3
1 1
0 3 4
1 2
Output
8 7
8 5
7 3 | [] | {"inputs": ["10 10\n5 1 2 4 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "8 5\n1 1 1 1 1 1 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 5\n1 1\n0 3 4\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 5\n1 1\n0 1 4\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 5\n1 1\n0 3 4\n1 3\n", "10 10\n5 1 2 8 1 7 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 7 3 8 10 8\n0 8 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "8 5\n2 1 1 1 1 1 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "8 5\n2 1 1 1 1 2 11 2\n1 1\n0 1 5\n1 1\n0 3 4\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 2\n1 1\n0 1 4\n1 2\n", "8 5\n1 1 1 1 2 2 4 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "10 10\n5 1 2 8 1 7 3 8 10 8\n0 8 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 3\n0 6 1\n1 9\n1 1\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 5\n1 1\n0 1 4\n1 4\n", "8 5\n1 1 1 1 1 4 11 2\n1 1\n0 1 5\n1 1\n0 1 7\n1 2\n", "10 10\n5 1 2 8 1 12 3 14 10 8\n0 4 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "8 5\n2 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "10 10\n7 1 2 16 1 12 3 10 10 16\n0 5 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "8 5\n1 1 1 2 1 2 11 2\n1 1\n0 1 5\n1 1\n0 3 4\n1 1\n", "8 5\n1 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 1\n", "10 10\n5 1 2 8 1 6 3 8 10 8\n0 8 2\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 3\n0 6 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 6 3 8 10 8\n0 8 2\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 1\n0 6 1\n1 9\n1 1\n", "8 5\n1 2 1 1 1 2 8 6\n1 1\n0 1 3\n1 1\n0 3 4\n1 1\n", "8 5\n1 1 1 1 1 1 8 2\n1 1\n0 1 3\n1 1\n0 3 3\n1 2\n", "10 10\n5 1 2 8 1 7 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 6\n1 2\n0 6 1\n1 9\n1 1\n", "8 5\n2 1 1 1 1 1 8 2\n1 1\n0 1 3\n1 1\n0 3 1\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 1\n0 1 5\n1 1\n0 1 7\n1 2\n", "10 10\n5 1 2 8 1 7 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 12 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 12 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "8 5\n1 1 1 1 1 2 4 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "10 10\n5 1 2 8 1 12 3 14 10 16\n0 5 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "8 5\n2 1 1 1 1 2 11 1\n1 1\n0 1 5\n1 1\n0 3 4\n1 2\n", "10 10\n5 1 2 8 1 12 3 10 10 16\n0 5 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 16 1 12 3 10 10 16\n0 5 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 4 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 2\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "10 10\n5 1 2 15 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "8 5\n1 1 1 2 1 1 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n", "8 5\n1 1 1 2 1 2 11 2\n1 1\n0 1 5\n1 1\n0 3 4\n1 2\n", "8 5\n1 1 1 1 1 2 11 2\n1 2\n0 1 5\n1 1\n0 3 4\n1 2\n", "10 10\n5 1 2 15 1 7 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 14 3 14 10 16\n0 5 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 6 3 8 10 8\n0 8 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 3\n0 6 1\n1 9\n1 1\n", "10 10\n5 1 2 4 1 7 3 8 10 8\n0 5 6\n1 8\n1 1\n0 10 4\n1 5\n1 3\n1 2\n0 6 1\n1 9\n1 1\n", "10 10\n5 1 2 15 1 7 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 5 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 12 3 14 14 8\n0 4 6\n1 8\n1 1\n0 10 6\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "10 10\n5 1 2 8 1 14 3 14 10 16\n0 5 6\n1 8\n1 1\n0 10 7\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "8 5\n1 1 1 1 1 2 8 3\n1 1\n0 1 3\n1 1\n0 3 4\n1 1\n", "8 5\n1 1 1 1 1 2 8 6\n1 1\n0 1 3\n1 1\n0 3 4\n1 1\n", "8 5\n1 1 1 1 1 2 18 2\n1 1\n0 1 5\n1 1\n0 1 7\n1 2\n", "8 5\n1 1 1 1 1 2 5 2\n1 1\n0 1 5\n1 1\n0 3 4\n1 3\n", "10 10\n5 1 2 6 1 7 3 14 10 8\n0 5 6\n1 8\n1 1\n0 10 3\n1 5\n1 3\n1 2\n0 4 1\n1 9\n1 1\n", "8 5\n1 1 1 1 1 2 8 2\n1 1\n0 1 3\n1 1\n0 3 4\n1 2\n"], "outputs": ["8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4\n", "7 7\n7 5\n7 3\n", "8 7\n8 5\n7 3\n", "8 7\n8 3\n7 3\n", "8 7\n8 3\n8 6\n", "8 7\n8 3\n7 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n6 2\n6 3\n6 4\n9 1\n10 4\n", "7 6\n7 5\n7 3\n", "8 6\n8 3\n7 3\n", "8 7\n8 6\n8 6\n", "7 6\n7 4\n7 3\n", "8 1\n6 2\n6 2\n6 3\n6 3\n9 1\n10 4\n", "8 7\n8 3\n8 4\n", "6 6\n6 2\n6 5\n", "8 1\n6 2\n6 2\n6 3\n6 4\n9 1\n6 2\n", "8 6\n8 5\n7 3\n", "8 1\n8 2\n5 1\n5 2\n5 3\n9 1\n8 2\n", "8 6\n8 3\n8 3\n", "8 7\n8 5\n8 5\n", "10 2\n6 2\n6 2\n6 3\n6 3\n9 1\n10 4\n", "10 2\n6 2\n6 2\n6 3\n6 2\n9 1\n10 4\n", "8 6\n8 5\n8 5\n", "7 7\n7 5\n7 4\n", "8 1\n6 2\n5 1\n6 1\n5 3\n9 1\n10 4\n", "7 6\n7 5\n7 6\n", "8 7\n8 3\n8 6\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 7\n8 5\n7 3\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 6\n8 3\n7 3\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4\n", "7 6\n7 4\n7 3\n", "8 6\n8 3\n7 3\n", "8 6\n8 3\n7 3\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n6 2\n6 3\n6 3\n9 1\n10 4\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n10 4\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 1\n6 2\n6 2\n6 3\n6 4\n9 1\n6 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 7\n8 5\n8 5\n", "8 7\n8 5\n8 5\n", "8 7\n8 3\n8 6\n", "8 7\n8 3\n7 2\n", "8 1\n6 2\n5 1\n5 2\n5 3\n9 1\n6 2\n", "8 7\n8 5\n7 3\n"]} | VERY_HARD | ['data structures', 'dsu'] | null | codeforces | ['Spanning trees', 'Data structures'] | ['Data structures'] | https://codeforces.com/problemset/problem/13/E | null | 1.0 seconds | null | null | 64.0 megabytes | null |
|
If you visit Aizu Akabeko shrine, you will find a unique paper fortune on which a number with more than one digit is written.
Each digit ranges from 1 to 9 (zero is avoided because it is considered a bad omen in this shrine). Using this string of numeric values, you can predict how many years it will take before your dream comes true. Cut up the string into more than one segment and compare their values. The difference between the largest and smallest value will give you the number of years before your wish will be fulfilled. Therefore, the result varies depending on the way you cut up the string. For example, if you are given a string 11121314 and divide it into segments, say, as 1,11,21,3,14, then the difference between the largest and smallest is 21 - 1 = 20. Another division 11,12,13,14 produces 3 (i.e. 14 - 11) years. Any random division produces a game of luck. However, you can search the minimum number of years using a program.
Given a string of numerical characters, write a program to search the minimum years before your wish will be fulfilled.
Input
The input is given in the following format.
n
An integer n is given. Its number of digits is from 2 to 100,000, and each digit ranges from 1 to 9.
Output
Output the minimum number of years before your wish will be fulfilled.
Examples
Input
11121314
Output
3
Input
123125129
Output
6
Input
119138
Output
5 | ["def sub(maxs, mins):\n\tfor i in range(len(maxs)):\n\t\tif maxs[i] != mins[i]:\n\t\t\tif i == len(maxs) - 1:\n\t\t\t\treturn int(maxs[i]) - int(mins[i])\n\t\t\tif i == len(maxs) - 2:\n\t\t\t\treturn int(maxs[i:i + 2]) - int(mins[i:i + 2])\n\t\t\treturn 10\n\treturn 0\n\ndef checkEqual(S):\n\tans = 8\n\tfor k in range(1, len(S)):\n\t\tif len(S) % k != 0:\n\t\t\tcontinue\n\t\tmins = maxs = S[0:k]\n\t\tfor s in range(0, len(S), k):\n\t\t\tmaxs = max(maxs, S[s:s + k])\n\t\t\tmins = min(mins, S[s:s + k])\n\t\tans = min(ans, sub(maxs, mins))\n\treturn ans\n\ndef check12(S):\n\tmaxv = 0\n\tminv = 10\n\tp = 0\n\twhile p < len(S):\n\t\tv = int(S[p])\n\t\tif S[p] == '1' and p + 1 < len(S):\n\t\t\tv = 10 + int(S[p + 1])\n\t\t\tp += 1\n\t\tmaxv = max(maxv, v)\n\t\tminv = min(minv, v)\n\t\tp += 1\n\treturn maxv - minv\nS = input()\nprint(min(checkEqual(S), check12(S)))\n", "n = input()\nlength = len(n)\nans = 10\nlst = []\nind = 0\nwhile ind < length:\n\tif n[ind] == '1' and ind + 1 <= length - 1:\n\t\tlst.append(int(n[ind:ind + 2]))\n\t\tind += 2\n\telse:\n\t\tlst.append(int(n[ind]))\n\t\tind += 1\nif len(lst) >= 2:\n\tans = min(ans, max(lst) - min(lst))\ndivisors = []\nfor i in range(1, length // 2 + 1):\n\tif length % i == 0:\n\t\tdivisors.append(i)\nfor i in divisors:\n\tlst = []\n\tfor j in range(0, length, i):\n\t\tlst.append(int(n[j:j + i]))\n\tans = min(ans, max(lst) - min(lst))\nprint(ans)\n"] | {"inputs": ["9714431", "16612328", "23422731", "754526", "955577", "75547", "2112", "799", "88", "32523857", "4787", "1859551", "135661", "3675", "156692", "167918384", "83994", "4837847", "14513597", "15282598", "12659326", "1468417", "6280", "115464", "52376853", "2315", "3641224", "97187", "836", "195884", "36250", "2427817", "17598762", "5744554", "9295", "129848", "3863342", "3743", "133862", "1237", "1625", "1179729", "12651", "3776912", "4829", "73", "2228", "2546", "3136", "138", "3380", "4828", "3652", "5667", "7275", "774", "9329", "279", "15119", "200", "2461", "19", "2258", "31", "1250", "1216", "1595", "271", "236", "187", "166", "123", "231272", "12342923", "16587352", "32887158", "42478456", "353843", "1884868", "148239", "54241537", "213811", "3614", "1003", "177127860", "54250", "1720310", "6415742", "12117", "1293", "5541389", "44936", "550", "43448", "664", "39426", "5003285", "73925", "4379155", "2270", "123125129", "119138", "11121314"], "outputs": ["8\n", "7\n", "6\n", "5\n", "4\n", "3\n", "1\n", "2\n", "0\n", "6\n", "4\n", "8\n", "5\n", "4\n", "8\n", "8\n", "6\n", "5\n", "8\n", "8\n", "8\n", "7\n", "8\n", "5\n", "6\n", "4\n", "5\n", "8\n", "5\n", "8\n", "6\n", "7\n", "8\n", "3\n", "3\n", "8\n", "6\n", "4\n", "7\n", "6\n", "5\n", "8\n", "5\n", "8\n", "7\n", "4\n", "6\n", "4\n", "5\n", "5\n", "8\n", "6\n", "4\n", "2\n", "3\n", "3\n", "7\n", "7\n", "6\n", "2\n", "5\n", "8\n", "6\n", "2\n", "5\n", "4\n", "8\n", "6\n", "4\n", "7\n", "5\n", "2\n", "6\n", "8\n", "7\n", "7\n", "6\n", "5\n", "7\n", "8\n", "6\n", "7\n", "5\n", "3\n", "8\n", "5\n", "7\n", "6\n", "5\n", "8\n", "8\n", "6\n", "5\n", "5\n", "2\n", "7\n", "8\n", "7\n", "8\n", "7\n", "6", "5", "3"]} | UNKNOWN_DIFFICULTY | [] | null | aizu | [] | [] | null | null | 1.0 seconds | null | null | 268.435456 megabytes | null |
|
You have a deck of $n$ cards, and you'd like to reorder it to a new one.
Each card has a value between $1$ and $n$ equal to $p_i$. All $p_i$ are pairwise distinct. Cards in a deck are numbered from bottom to top, i. e. $p_1$ stands for the bottom card, $p_n$ is the top card.
In each step you pick some integer $k > 0$, take the top $k$ cards from the original deck and place them, in the order they are now, on top of the new deck. You perform this operation until the original deck is empty. (Refer to the notes section for the better understanding.)
Let's define an order of a deck as $\sum\limits_{i = 1}^{n}{n^{n - i} \cdot p_i}$.
Given the original deck, output the deck with maximum possible order you can make using the operation above.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \le n \le 10^5$) — the size of deck you have.
The second line contains $n$ integers $p_1, p_2,\dots, p_n$ ($1 \le p_i \le n$; $p_i \neq p_j$ if $i \neq j$) — values of card in the deck from bottom to top.
It's guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$.
-----Output-----
For each test case print the deck with maximum possible order. Print values of cards in the deck from bottom to top.
If there are multiple answers, print any of them.
-----Examples-----
Input
4
4
1 2 3 4
5
1 5 2 4 3
6
4 2 5 3 6 1
1
1
Output
4 3 2 1
5 2 4 3 1
6 1 5 3 4 2
1
-----Note-----
In the first test case, one of the optimal strategies is the next one:
take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes $[1, 2, 3]$, $p'$ becomes $[4]$;
take $1$ card from the top of $p$: $p$ becomes $[1, 2]$, $p'$ becomes $[4, 3]$;
take $1$ card from the top of $p$: $p$ becomes $[1]$, $p'$ becomes $[4, 3, 2]$;
take $1$ card from the top of $p$: $p$ becomes empty, $p'$ becomes $[4, 3, 2, 1]$.
In result, $p'$ has order equal to $4^3 \cdot 4 + 4^2 \cdot 3 + 4^1 \cdot 2 + 4^0 \cdot 1$ $=$ $256 + 48 + 8 + 1 = 313$.
In the second test case, one of the optimal strategies is:
take $4$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[1]$, $p'$ becomes $[5, 2, 4, 3]$;
take $1$ card from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[5, 2, 4, 3, 1]$;
In result, $p'$ has order equal to $5^4 \cdot 5 + 5^3 \cdot 2 + 5^2 \cdot 4 + 5^1 \cdot 3 + 5^0 \cdot 1$ $=$ $3125 + 250 + 100 + 15 + 1 = 3491$.
In the third test case, one of the optimal strategies is:
take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2, 5, 3]$, $p'$ becomes $[6, 1]$;
take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes $[4, 2]$, $p'$ becomes $[6, 1, 5, 3]$;
take $2$ cards from the top of $p$ and move it to $p'$: $p$ becomes empty, $p'$ becomes $[6, 1, 5, 3, 4, 2]$.
In result, $p'$ has order equal to $6^5 \cdot 6 + 6^4 \cdot 1 + 6^3 \cdot 5 + 6^2 \cdot 3 + 6^1 \cdot 4 + 6^0 \cdot 2$ $=$ $46656 + 1296 + 1080 + 108 + 24 + 2 = 49166$. | ["import heapq\nfrom math import sqrt\nimport operator\nimport sys\ninf_var = 0\nif inf_var == 1:\n\tinf = open('input.txt', 'r')\nelse:\n\tinf = sys.stdin\ninput = inf.readline\n\ndef read_one_int():\n\treturn int(input().rstrip('\\n'))\n\ndef read_list_of_ints():\n\tres = [int(val) for val in input().rstrip('\\n').split(' ')]\n\treturn res\n\ndef read_str():\n\treturn input().rstrip()\n\ndef check_seq(deck_size, deck_cards):\n\tnew_deck = []\n\tused = [0 for i in range(deck_size)]\n\tlast_used_index = deck_size - 1\n\tprev_ind = deck_size\n\tfor i in range(deck_size - 1, -1, -1):\n\t\tif deck_cards[i] == last_used_index + 1:\n\t\t\tnew_deck += deck_cards[i:prev_ind]\n\t\t\tfor j in range(i, prev_ind):\n\t\t\t\tused[deck_cards[j] - 1] = 1\n\t\t\tprev_ind = i\n\t\t\tj = -1\n\t\t\twhile True:\n\t\t\t\tcur_ind = j + last_used_index\n\t\t\t\tif cur_ind < 0:\n\t\t\t\t\tlast_used_index = -1\n\t\t\t\t\tbreak\n\t\t\t\tif used[cur_ind]:\n\t\t\t\t\tj -= 1\n\t\t\t\t\tcontinue\n\t\t\t\telse:\n\t\t\t\t\tlast_used_index = cur_ind\n\t\t\t\t\tbreak\n\treturn ' '.join(map(str, new_deck))\n\ndef main():\n\tcnt = read_one_int()\n\tfor _ in range(cnt):\n\t\tdeck_size = read_one_int()\n\t\tdeck_cards = read_list_of_ints()\n\t\tres = check_seq(deck_size, deck_cards)\n\t\tprint(res)\nmain()\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tans = []\n\tp1 = [-1] * (n + 1)\n\tfor i in range(n):\n\t\tp1[p[i]] = i\n\ti = n\n\twhile i:\n\t\twhile i > 0 and p1[i] == -1:\n\t\t\ti -= 1\n\t\telse:\n\t\t\tif i:\n\t\t\t\tk = 0\n\t\t\t\tfor j in range(p1[i], n):\n\t\t\t\t\tans.append(p[j])\n\t\t\t\t\tp1[p[j]] = -1\n\t\t\t\t\tk += 1\n\t\t\t\tn -= k\n\t\t\t\ti -= 1\n\t\t\telse:\n\t\t\t\tbreak\n\tprint(*ans)\n", "import sys\n\ndef get_ints():\n\treturn map(int, sys.stdin.readline().strip().split())\n\ndef get_list():\n\treturn list(map(int, sys.stdin.readline().strip().split()))\n\ndef get_list_string():\n\treturn list(map(str, sys.stdin.readline().strip().split()))\n\ndef get_string():\n\treturn sys.stdin.readline().strip()\n\ndef get_int():\n\treturn int(sys.stdin.readline().strip())\n\ndef get_print_int(x):\n\tsys.stdout.write(str(x) + '\\n')\n\ndef get_print(x):\n\tsys.stdout.write(x + '\\n')\n\ndef get_print_int_same(x):\n\tsys.stdout.write(str(x) + ' ')\n\ndef get_print_same(x):\n\tsys.stdout.write(x + ' ')\nfrom sys import maxsize\n\ndef solve():\n\tfor _ in range(get_int()):\n\t\tn = get_int()\n\t\tarr = get_list()\n\t\ti = n - 1\n\t\tj = n - 1\n\t\ttemp = sorted(arr)\n\t\tvis = [False] * n\n\t\tans = []\n\t\twhile j >= 0:\n\t\t\tt = j\n\t\t\ttt = []\n\t\t\twhile t >= 0 and arr[t] != temp[i]:\n\t\t\t\tvis[arr[t] - 1] = True\n\t\t\t\ttt.append(arr[t])\n\t\t\t\tt -= 1\n\t\t\tvis[arr[t] - 1] = True\n\t\t\ttt.append(arr[t])\n\t\t\ttt = tt[::-1]\n\t\t\tfor k in tt:\n\t\t\t\tans.append(k)\n\t\t\tj = t - 1\n\t\t\twhile i >= 0 and vis[i]:\n\t\t\t\ti -= 1\n\t\tget_print(' '.join(map(str, ans)))\nsolve()\n", "from heapq import heappop, heappush\nimport sys\n\nclass MinMaxSet:\n\n\tdef __init__(self):\n\t\tself.min_queue = []\n\t\tself.max_queue = []\n\t\tself.entries = {}\n\n\tdef __len__(self):\n\t\treturn len(self.entries)\n\n\tdef add(self, val):\n\t\tif val not in self.entries:\n\t\t\tentry_min = [val, False]\n\t\t\tentry_max = [-val, False]\n\t\t\theappush(self.min_queue, entry_min)\n\t\t\theappush(self.max_queue, entry_max)\n\t\t\tself.entries[val] = (entry_min, entry_max)\n\n\tdef delete(self, val):\n\t\tif val in self.entries:\n\t\t\t(entry_min, entry_max) = self.entries.pop(val)\n\t\t\tentry_min[-1] = entry_max[-1] = True\n\n\tdef get_min(self):\n\t\twhile self.min_queue[0][-1]:\n\t\t\theappop(self.min_queue)\n\t\treturn self.min_queue[0][0]\n\n\tdef get_max(self):\n\t\twhile self.max_queue[0][-1]:\n\t\t\theappop(self.max_queue)\n\t\treturn -self.max_queue[0][0]\nt = int(input())\nwhile t > 0:\n\tn = int(sys.stdin.readline())\n\ta = list(map(int, sys.stdin.readline().split()))\n\tused = [0] * n\n\tpos = [0] * (n + 1)\n\tans = list()\n\ts = MinMaxSet()\n\tfor i in range(n):\n\t\ts.add(a[i])\n\t\tpos[a[i]] = i\n\twhile len(s) > 0:\n\t\tx = s.get_max()\n\t\tfor j in range(pos[x], n):\n\t\t\tif used[j] > 0:\n\t\t\t\tbreak\n\t\t\tused[j] = 1\n\t\t\ts.delete(a[j])\n\t\t\tans.append(a[j])\n\tprint(*ans)\n\tt -= 1\n", "import sys\ninput = sys.stdin.readline\nfor _ in range(int(input())):\n\tn = int(input())\n\tw = list(map(int, input().split()))\n\td = [0] * (n + 1)\n\tfor (i, j) in enumerate(w):\n\t\td[j] = i\n\t(a, x) = ([], n)\n\tfor i in range(n, 0, -1):\n\t\tif d[i] < x:\n\t\t\ta.extend(w[d[i]:x])\n\t\t\tx = d[i]\n\tprint(' '.join(map(str, a)))\n", "def argmax(a):\n\tm = 0\n\tres = []\n\tfor j in range(len(a)):\n\t\tif a[j] > m:\n\t\t\tm = a[j]\n\t\t\tres.append(j)\n\tres.reverse()\n\treturn res\n\ndef find():\n\tend = int(input())\n\tmas = list(map(int, input().split()))\n\tfor j in argmax(mas):\n\t\tfor k in range(j, end):\n\t\t\tprint(mas[k], end=' ')\n\t\tend = j\n\tprint()\nfor i in range(int(input())):\n\tfind()\n", "t = int(input())\nfor i in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tp_ord = p.copy()\n\tp_ord.sort()\n\tk = n - 1\n\tr = list()\n\tfor j in range(n - 1, -1, -1):\n\t\twhile p_ord[k] == 0:\n\t\t\tk -= 1\n\t\tmaximo = p_ord[k]\n\t\tp_ord[p[j] - 1] = 0\n\t\tif p[j] == maximo:\n\t\t\tr.extend(p[j:])\n\t\t\tdel p[j:]\n\tprint(' '.join(map(str, r)))\n", "A = []\n\ndef test_case():\n\tn = int(input())\n\ta = [int(i) for i in input().split()]\n\tmp = dict()\n\tfor i in range(n):\n\t\tmp[a[i]] = i\n\t(ans, last) = ([], n)\n\tfor i in range(n, 0, -1):\n\t\tif mp[i] <= last:\n\t\t\tans.extend(a[mp[i]:last])\n\t\t\tlast = mp[i]\n\tA.append(ans)\nfor _ in range(int(input())):\n\ttest_case()\nfor a in A:\n\tprint(*a)\n", "t = int(input())\nwhile t > 0:\n\tt -= 1\n\tn = int(input())\n\tar = [int(op) for op in input().split()]\n\tans = []\n\ty = [0 for i in range(n)]\n\tmx = n\n\tnmx = n\n\tops = n\n\twhile not nmx == 0:\n\t\tfor i in reversed(range(ops)):\n\t\t\tif y[i] == 0:\n\t\t\t\tmx = i + 1\n\t\t\t\ty[i] = 1\n\t\t\t\tops = i\n\t\t\t\tbreak\n\t\tfor i in reversed(range(nmx)):\n\t\t\tif mx == ar[i]:\n\t\t\t\tidx = i\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\ty[ar[i] - 1] = 1\n\t\tfor i in range(nmx - idx):\n\t\t\tans.append(str(ar[i + idx]))\n\t\tnmx = idx\n\tprint(' '.join(ans))\n", "for _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tlook = [0] * n\n\tmaxx = arr[0]\n\tfor i in range(n):\n\t\tmaxx = max(arr[i], maxx)\n\t\tlook[i] = maxx\n\tj = n\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif look[i] == arr[i]:\n\t\t\tans.append(arr[i:j])\n\t\t\tj = i\n\tfor i in ans:\n\t\tprint(*i, end=' ')\n\tprint()\n", "N = int(input())\nfor _ in range(N):\n\tout = []\n\tn = int(input())\n\tl = [int(e) for e in input().split()]\n\ti = 0\n\tfor j in range(i, n):\n\t\tif l[j] > l[i]:\n\t\t\tout += l[i:j][::-1]\n\t\t\ti = j\n\tout += l[i:n][::-1]\n\tprint(' '.join([str(e) for e in out[::-1]]))\n", "from collections import OrderedDict\nimport heapq as hq\n\ndef show(l):\n\tfor i in l:\n\t\tprint(i, end=' ')\nfor _ in range(int(input())):\n\tn = int(input())\n\tr = []\n\tl = list(map(int, input().split()))\n\tt = [(j, i) for (i, j) in enumerate(l)]\n\tt.sort(reverse=True)\n\tod = OrderedDict(t)\n\tidx = n\n\tfor e in l[::-1]:\n\t\tm = next(iter(od))\n\t\tif e == m:\n\t\t\tshow(l[od[e]:idx])\n\t\t\tidx = od[e]\n\t\tdel od[e]\n\tprint()\n", "import math\nfor _ in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tb = {}\n\tans = []\n\tfor i in range(n):\n\t\tb[a[i]] = i\n\tflag = n\n\tfor j in range(n, 0, -1):\n\t\tif b[j] <= flag:\n\t\t\tfor k in range(b[j], flag):\n\t\t\t\tans.append(a[k])\n\t\t\tflag = b[j]\n\tprint(*ans)\n", "from collections import deque\nt = int(input())\nfor _ in range(t):\n\tc = int(input())\n\tstack = list(map(int, input().split()))\n\tans = deque()\n\tflag = 0\n\tgreatest = stack[0]\n\tfor i in range(1, c):\n\t\tif greatest < stack[i]:\n\t\t\tans.extendleft(reversed(stack[flag:i]))\n\t\t\tflag = i\n\t\t\tgreatest = stack[i]\n\tans.extendleft(reversed(stack[flag:c]))\n\tprint(*ans, sep=' ')\n", "t = int(input())\nwhile t > 0:\n\tt -= 1\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tans = []\n\tr = [0] * (n + 1)\n\tfor i in range(n):\n\t\tr[a[i]] = i\n\tk = n\n\tfor i in range(n, 0, -1):\n\t\tif r[i] <= k:\n\t\t\tfor j in range(r[i], k):\n\t\t\t\tans.append(a[j])\n\t\t\tk = r[i]\n\tprint(*ans)\n", "t = int(input())\nfor i in range(t):\n\tn = int(input())\n\tarr = list(map(int, input().split(' ')))\n\tans = []\n\ttemp = []\n\tGreater = [arr[0]]\n\tfor k in range(1, n):\n\t\tif Greater[k - 1] > arr[k]:\n\t\t\tGreater.append(Greater[k - 1])\n\t\telse:\n\t\t\tGreater.append(arr[k])\n\tfor j in range(len(arr) - 1, -1, -1):\n\t\tif arr[j] != Greater[j]:\n\t\t\ttemp.append(arr[j])\n\t\telse:\n\t\t\ttemp.append(arr[j])\n\t\t\tans += temp[::-1]\n\t\t\ttemp = []\n\tprint(' '.join(map(str, ans)))\n", "quant_testes = int(input())\nfor c in range(quant_testes):\n\toutput = ''\n\ttam = int(input())\n\tseq = [int(n) for n in input().split()]\n\tposicoes = [None] * tam\n\tfor i in range(len(seq)):\n\t\tposicoes[-seq[i]] = i\n\tfor pos in posicoes:\n\t\tif pos + 1 <= tam:\n\t\t\toutput += str(seq[pos])\n\t\t\toutput += ' '\n\t\t\tfor i in range(pos + 1, tam):\n\t\t\t\toutput += str(seq[i])\n\t\t\t\toutput += ' '\n\t\t\t\ttam -= 1\n\t\t\ttam -= 1\n\tprint(output)\n", "T = int(input())\nfor t in range(T):\n\tn = int(input())\n\tpi = list(map(int, input().split()))\n\tr = []\n\tt = [0] * len(pi)\n\tt[0] = pi[0]\n\tfor i in range(1, len(pi)):\n\t\tt[i] = max(t[i - 1], pi[i])\n\tindex = len(pi) - 1\n\tlastIndex = len(pi)\n\twhile index >= 0:\n\t\twhile index >= 0 and pi[index] != t[index]:\n\t\t\tindex -= 1\n\t\tif pi[index] == t[index]:\n\t\t\tr += pi[index:lastIndex]\n\t\t\tlastIndex = index\n\t\tindex -= 1\n\tprint(' '.join(map(str, r)))\n", "import sys\ninput = sys.stdin.readline\nfor _ in range(int(input())):\n\tn = int(input())\n\tli = list(map(int, input().split()))\n\tbigs = [0]\n\tcurrent_max = li[0]\n\tfor i in range(1, n):\n\t\tif li[i] > current_max:\n\t\t\tcurrent_max = li[i]\n\t\t\tbigs.append(i)\n\tbigs = reversed(bigs)\n\tans = []\n\tfor start in bigs:\n\t\tfor j in range(start, n):\n\t\t\tans.append(li[j])\n\t\tn = start\n\tprint(*ans)\n", "import sys\nimport os.path\nfrom collections import *\nimport math\nimport bisect\nif os.path.exists('input.txt'):\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nelse:\n\tinput = sys.stdin.readline\nt = int(input())\nwhile t:\n\tt -= 1\n\tn = int(input())\n\tp = [int(x) for x in input().split()]\n\tarr = [0] * n\n\tmaxval = 0\n\tfor i in range(n):\n\t\tmaxval = max(p[i], maxval)\n\t\tarr[i] = (p[i], maxval)\n\tarr.sort(key=lambda x: x[1], reverse=True)\n\tfor i in range(n):\n\t\tprint(arr[i][0], end=' ')\n\tprint()\n", "n = int(input())\nfor i in range(n):\n\tm = int(input())\n\ta = list(map(int, input().split()))\n\tmaxrest = [0 for i in range(m)]\n\tfor j in range(1, m):\n\t\tif a[j] > a[maxrest[j - 1]]:\n\t\t\tmaxrest[j] = j\n\t\telse:\n\t\t\tmaxrest[j] = maxrest[j - 1]\n\trest = m\n\twhile rest != 0:\n\t\tnewrest = maxrest[rest - 1]\n\t\tfor j in range(newrest, rest):\n\t\t\tprint(a[j], end=' ')\n\t\trest = newrest\n", "t = int(input())\nfor j in range(t):\n\tans = dict()\n\tk = int(input())\n\td = list(map(int, input().split()))\n\tma = d[0]\n\tans[0] = ma\n\tfor i in range(1, k):\n\t\tif d[i] > ma:\n\t\t\tma = d[i]\n\t\t\tans[i] = ma\n\tans = list(reversed(ans.keys()))\n\tb = []\n\tend = len(ans)\n\tfor i in range(end):\n\t\tp = ans[i]\n\t\tb += d[p:k]\n\t\tk = p\n\tprint(*b)\n", "import sys\ninput = sys.stdin.readline\n\ndef solve():\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tS = set()\n\tmv = n\n\ttmp = []\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\ttmp.append(arr[i])\n\t\tS.add(arr[i])\n\t\tif arr[i] == mv:\n\t\t\twhile tmp:\n\t\t\t\tans.append(tmp.pop())\n\t\t\twhile mv in S:\n\t\t\t\tmv -= 1\n\treturn ans\nfor _ in range(int(input())):\n\tprint(*solve())\n", "for s in [*open(0)][2::2]:\n\t(*l,) = map(int, s.split())\n\ta = []\n\tj = len(l)\n\ta = []\n\tL = [0]\n\tfor i in range(1, j):\n\t\tif l[L[-1]] < l[i]:\n\t\t\tL += [i]\n\t\telse:\n\t\t\tL += [L[-1]]\n\twhile j:\n\t\ti = L[j - 1]\n\t\ta += l[i:j]\n\t\tj = i\n\tprint(*a)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tli = list(map(int, input().split()))\n\ttmp = [0] * (n + 1)\n\tres = []\n\tfor i in range(n):\n\t\ttmp[li[i]] = i\n\tk = n\n\tfor i in range(n, 0, -1):\n\t\tif tmp[i] <= k:\n\t\t\tfor j in range(tmp[i], k):\n\t\t\t\tres.append(li[j])\n\t\t\tk = tmp[i]\n\tprint(*res)\n", "from sys import stdin\nlst = list(map(int, stdin.read().split()))\n_s = 0\n\ndef inp(n=1):\n\tglobal _s\n\tret = lst[_s:_s + n]\n\t_s += n\n\treturn ret\n\ndef inp1():\n\treturn inp()[0]\nt = inp1()\nfor _ in range(t):\n\tn = inp1()\n\tc = inp(n)\n\tnew = []\n\tfor i in range(n):\n\t\tif len(new) and c[i] < new[-1][0]:\n\t\t\tnew[-1].append(c[i])\n\t\telse:\n\t\t\tnew.append([c[i]])\n\tnew.reverse()\n\tfor i in new:\n\t\tfor j in i:\n\t\t\tprint(j, end=' ')\n\tprint()\n", "for _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\ta = [0] * n\n\tt = []\n\tfor i in range(n):\n\t\ta[arr[i] - 1] = i\n\tf = n + 1\n\tfor i in range(len(a) - 1, -1, -1):\n\t\tif a[i] > f:\n\t\t\tcontinue\n\t\tt.append(arr[a[i]:f])\n\t\tf = a[i]\n\tfor i in range(len(t)):\n\t\tfor j in range(len(t[i])):\n\t\t\tprint(t[i][j], end=' ')\n\tprint()\n", "def take_second(elem):\n\treturn elem[0]\nq = int(input())\nwhile q > 0:\n\tn = int(input())\n\ta = input().split()\n\ta = [int(x) for x in a]\n\tlist = []\n\ti = 0\n\twhile i < n:\n\t\tif i == 0 or a[i] > a[i - 1]:\n\t\t\tlist.append([a[i]])\n\t\t\tlist[-1].append(i)\n\t\ti += 1\n\tlist = sorted(list, key=take_second)\n\tb = [0 for i in range(n)]\n\tans = []\n\ti = len(list) - 1\n\twhile i >= 0:\n\t\tfor j in range(list[i][1], n):\n\t\t\tif b[j] == 0:\n\t\t\t\tb[j] = 1\n\t\t\t\tans.append(a[j])\n\t\t\telse:\n\t\t\t\tbreak\n\t\ti -= 1\n\tfor i in ans:\n\t\tprint(i, end=' ')\n\tprint()\n\tq = q - 1\n", "def solve(arr, size):\n\tpositions = [0] * size\n\tfor j in range(size):\n\t\tpositions[arr[j] - 1] = j\n\tK = [positions[size - 1]]\n\tfor j in range(size - 2, -1, -1):\n\t\tif positions[j] < K[-1]:\n\t\t\tK.append(positions[j])\n\tresult = [0] * size\n\tright = size\n\tpos = 0\n\tfor left in K:\n\t\tfor j in range(right - left):\n\t\t\tresult[pos] = arr[left + j]\n\t\t\tpos += 1\n\t\tright = left\n\tfor el in result:\n\t\tprint(el, end=' ')\n\tprint('')\nN = int(input())\nfor n in range(N):\n\tL = int(input())\n\tA = [int(x) for x in input().split()]\n\tsolve(A, L)\n", "import sys\ninput = sys.stdin.readline\nfor _ in range(int(input().strip())):\n\tn = int(input().strip())\n\ta = list(map(int, input().strip().split(' ')))\n\tasc = set(a)\n\te = n\n\to = []\n\tfor i in range(n, 0, -1):\n\t\tif i in asc:\n\t\t\tfor j in range(e - 1, -1, -1):\n\t\t\t\tif a[j] == i:\n\t\t\t\t\tfor k in a[j:e]:\n\t\t\t\t\t\tasc.remove(k)\n\t\t\t\t\to += a[j:e]\n\t\t\t\t\te = j\n\t\t\t\t\tbreak\n\tprint(' '.join(map(str, o)))\n", "for t in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tlook = [0] * n\n\tlook[0] = a[0]\n\tfor i in range(1, n):\n\t\tlook[i] = max(look[i - 1], a[i])\n\tj = n\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif look[i] == a[i]:\n\t\t\tans.extend(a[i:j])\n\t\t\tj = i\n\tprint(*ans)\n", "import sys\ninput = sys.stdin.readline\nimport math\nimport bisect\nfrom copy import deepcopy as dc\nfrom itertools import accumulate\nfrom collections import Counter, defaultdict, deque\n\ndef ceil(U, V):\n\treturn (U + V - 1) // V\n\ndef modf1(N, MOD):\n\treturn (N - 1) % MOD + 1\ninf = int(1e+18)\nmod = int(1000000000.0 + 7)\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tpc = list(accumulate(p, func=max))\n\tod = []\n\tprev = n\n\tfor i in range(n - 1, -1, -1):\n\t\tif pc[i] == p[i]:\n\t\t\tfor j in range(i, prev):\n\t\t\t\tod.append(p[j])\n\t\t\t\tprev = i\n\tfor j in range(prev):\n\t\tod.append(p[j])\n\tprint(*od)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\n\ndef main():\n\tfor _ in range(int(input())):\n\t\tn = int(input())\n\t\ta = list(map(int, input().split()))\n\t\tp = [a[0]]\n\t\tfor i in range(1, n):\n\t\t\tp.append(max(p[-1], a[i]))\n\t\tb = [0] * (n + 1)\n\t\tfor (i, v) in enumerate(a):\n\t\t\tb[v] = i\n\t\tans = []\n\t\ti = n - 1\n\t\twhile p:\n\t\t\tj = b[p[-1]]\n\t\t\tfor k in range(j, i + 1):\n\t\t\t\tans.append(a[k])\n\t\t\t\tp.pop()\n\t\t\ti = j - 1\n\t\tprint(*ans)\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nmain()\n", "for i in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tb = []\n\tc = [0 for j in range(n)]\n\td = n\n\te = n\n\tfor j in range(n - 1, -1, -1):\n\t\tc[a[j] - 1] = 1\n\t\tif a[j] == d:\n\t\t\tb += a[j:e]\n\t\t\te = j\n\t\t\twhile d > 0 and c[d - 1] == 1:\n\t\t\t\td -= 1\n\tb += a[:e]\n\tprint(*b)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tk = []\n\td = {}\n\tfor i in range(len(l)):\n\t\td[l[i]] = i\n\tt = int(n)\n\tfor i in range(n, 0, -1):\n\t\tif d[i] <= t:\n\t\t\tfor j in range(d[i], t):\n\t\t\t\tk.append(l[j])\n\t\t\tt = d[i]\n\tprint(*k)\n", "def card(n, arr):\n\tind = [0] * n\n\ttemp = n\n\tans = []\n\tfor i in range(n):\n\t\tind[arr[i] - 1] = i\n\tfor i in ind[::-1]:\n\t\tif i < temp:\n\t\t\tans += arr[i:temp]\n\t\t\ttemp = i\n\treturn ans\nfor i in range(int(input())):\n\ta = int(input())\n\tlst = list(map(int, input().strip().split()))\n\tprint(*card(a, lst))\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tdic = {}\n\tresult = []\n\tfor i in range(n):\n\t\tdic[l[i]] = i\n\ttemp = n\n\tfor i in range(n, 0, -1):\n\t\tif dic[i] < temp:\n\t\t\tresult.extend(l[dic[i]:temp])\n\t\t\ttemp = dic[i]\n\tprint(*result)\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tdec = list(map(int, input().split()))\n\tdic = {}\n\tfor i in range(n):\n\t\tdic[dec[i]] = i\n\tcovered_till = n\n\tnew_dec = []\n\tfor i in range(n, 0, -1):\n\t\tif dic[i] < covered_till:\n\t\t\tnew_dec += dec[dic[i]:covered_till]\n\t\t\tcovered_till = dic[i]\n\tprint(*new_dec)\n", "from collections import defaultdict\nfor _ in range(int(input())):\n\tn = int(input())\n\tv = list(map(int, input().split()))\n\tcnt = 0\n\tj = n - 1\n\ti = n - 1\n\tcur_max = n\n\ttemp = []\n\twhile cnt < n:\n\t\twhile v[i] != cur_max:\n\t\t\ttemp.append(v[i])\n\t\t\ti -= 1\n\t\ttemp.append(v[i])\n\t\ttemp.sort(reverse=True)\n\t\tflag = 1\n\t\tfor x in range(1, len(temp)):\n\t\t\tif temp[x - 1] - temp[x] > 1:\n\t\t\t\tcur_max = temp[x - 1] - 1\n\t\t\t\tflag = 0\n\t\t\t\ttemp = temp[x:]\n\t\t\t\tbreak\n\t\tif flag:\n\t\t\tcur_max = temp[len(temp) - 1] - 1\n\t\t\ttemp = []\n\t\telif flag != 0 and len(temp) == 1:\n\t\t\tcur_max -= 1\n\t\t\ttemp = []\n\t\tfor k in range(i, j + 1):\n\t\t\tprint(v[k], end=' ')\n\t\t\tcnt += 1\n\t\tj = i - 1\n\t\ti -= 1\n\tprint('')\n", "for _ in range(int(input())):\n\tn = int(input())\n\tps = list(map(int, input().split()))\n\tdp = [ps[0]]\n\tfor i in range(1, n):\n\t\tdp.append(max(dp[-1], ps[i]))\n\tres = []\n\tj = n - 1\n\ttemp = [ps[-1]]\n\tfor i in range(n - 2, -1, -1):\n\t\tif dp[i] == dp[i + 1]:\n\t\t\ttemp.append(ps[i])\n\t\telse:\n\t\t\tres += temp[::-1]\n\t\t\ttemp = [ps[i]]\n\tres += temp[::-1]\n\tprint(*res)\n", "from collections import deque\nn = int(input())\nfor _ in range(n):\n\tc = int(input())\n\td = list(map(int, input().split()))\n\tanswer = deque()\n\tcount = 0\n\tgreatest = d[0]\n\tfor i in range(1, c):\n\t\tif greatest < d[i]:\n\t\t\tanswer.extendleft(reversed(d[count:i]))\n\t\t\tcount = i\n\t\t\tgreatest = d[i]\n\tanswer.extendleft(reversed(d[count:c]))\n\tprint(*answer, sep=' ')\n", "def cardDeck(n, array):\n\tarray.reverse()\n\tstack = []\n\tfor x in range(n):\n\t\tif not stack:\n\t\t\tstack.append(x)\n\t\telse:\n\t\t\twhile stack and array[x] > array[stack[-1]]:\n\t\t\t\tstack.pop()\n\t\t\tstack.append(x)\n\tans = []\n\tprev = 0\n\tans += array[stack[0]::-1]\n\tfor x in range(1, len(stack)):\n\t\tans += array[stack[x]:stack[x - 1]:-1]\n\t\tprev = stack[x]\n\treturn ans\nt = int(input())\nfinal = []\nfor _ in range(t):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tfinal.append(cardDeck(n, l))\nfor _ in range(t):\n\tfor x in final[_]:\n\t\tprint(x, end=' ')\n\tprint()\n", "from collections import Counter, deque\nfrom math import *\nmod = 998244353\n\ndef solve():\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tval = [i + 1 for i in range(n)]\n\tcur = val[-1]\n\tans = []\n\tx = n - 1\n\tz = n - 1\n\twhile x >= 0:\n\t\td = deque()\n\t\twhile l[x] != cur:\n\t\t\ty = l.pop()\n\t\t\tval[y - 1] = -1\n\t\t\td.appendleft(y)\n\t\t\tx -= 1\n\t\ty = l.pop()\n\t\td.appendleft(y)\n\t\tans += d\n\t\tx -= 1\n\t\tval[y - 1] = -1\n\t\twhile z >= 0 and val[z] == -1:\n\t\t\tz -= 1\n\t\tcur = val[z]\n\tprint(*ans)\nt = int(input())\nfor _ in range(t):\n\tsolve()\n", "import sys\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\ninp = lambda : list(map(int, sys.stdin.readline().rstrip('\\r\\n').split()))\nmod = 10 ** 9 + 7\nMod = 998244353\nINF = float('inf')\nfrom heapq import *\ntc = 1\n(tc,) = inp()\nfor _ in range(tc):\n\t(n,) = inp()\n\ta = inp()\n\tvis = [False] * n\n\th = [(-a[i], i) for i in range(n)]\n\theapify(h)\n\tans = []\n\twhile h:\n\t\t(node, ind) = heappop(h)\n\t\tif vis[ind] == True:\n\t\t\tcontinue\n\t\tfor i in range(ind, n):\n\t\t\tif vis[i] == True:\n\t\t\t\tbreak\n\t\t\tvis[i] = True\n\t\t\tans.append(a[i])\n\tprint(*ans)\n", "def solve():\n\tn = int(input())\n\tA = list(map(int, input().split()))\n\tB = [0] * (n + 1)\n\ttop = n\n\tpos = n\n\t(l, r) = (0, n)\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif A[i] == top:\n\t\t\tfor x in range(i, r):\n\t\t\t\tans.append(A[x])\n\t\t\t\tB[A[x]] = 1\n\t\t\tr = i\n\t\t\twhile B[top]:\n\t\t\t\ttop -= 1\n\t\t\tcontinue\n\treturn ans\nfor i in range(int(input())):\n\tprint(*solve())\n", "t = int(input())\nfor i in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tl = [i for i in range(n, 0, -1)]\n\tj = n - 1\n\tk = 0\n\tb = n - 1\n\ta = []\n\twhile j >= 0:\n\t\tif l[k] > p[j] and l[k] != -1:\n\t\t\tl[n - p[j]] = -1\n\t\t\tj = j - 1\n\t\telif l[k] == p[j] and l[k] != -1:\n\t\t\ta += p[j:b + 1]\n\t\t\tj = j - 1\n\t\t\tb = j\n\t\t\tk = k + 1\n\t\telif l[k] == -1:\n\t\t\tk = k + 1\n\tprint(*a)\n", "def carddeckV2():\n\tnbCase = int(input())\n\tres = []\n\tfor i in range(nbCase):\n\t\tl_desk = int(input())\n\t\tcase = [int(j) for j in input().split(' ')]\n\t\tM = case[0]\n\t\tlast = 0\n\t\tp_prime = []\n\t\tfor i in range(len(case)):\n\t\t\tif case[i] > M:\n\t\t\t\tfor j in reversed(range(last, i)):\n\t\t\t\t\tp_prime.append(case[j])\n\t\t\t\tlast = i\n\t\t\t\tM = case[i]\n\t\tfor j in reversed(range(last, len(case))):\n\t\t\tp_prime.append(case[j])\n\t\tres.append(p_prime)\n\tfor r in res:\n\t\ts = ''\n\t\tfor i in reversed(range(len(r))):\n\t\t\ts += str(r[i])\n\t\t\ts += ' '\n\t\tprint(s)\ncarddeckV2()\n", "def solve(nums):\n\th = {num: i for (i, num) in enumerate(nums)}\n\tm = [h[str(num)] for num in range(len(nums), 0, -1)]\n\tresult = []\n\ti_max = len(nums)\n\tfor i in m:\n\t\tif i < i_max:\n\t\t\tresult += nums[i:i_max]\n\t\t\ti_max = i\n\tprint(' '.join(map(str, result)))\nt = int(input().strip())\nwhile t:\n\tt -= 1\n\tinput()\n\tsolve(input().split())\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\td = {}\n\tfor i in range(n):\n\t\td[p[i]] = i\n\tout = []\n\tpv = n\n\tfor i in range(n, 0, -1):\n\t\tif d[i] <= pv:\n\t\t\tout += p[d[i]:pv]\n\t\t\tpv = d[i]\n\tprint(*out)\n", "import sys, functools, collections, bisect, math\ninput = sys.stdin.readline\nimport heapq\nt = int(input())\nfor _ in range(t):\n\tn = int(input().strip())\n\tarr = list(map(int, input().strip().split()))\n\tmaxarr = [(arr[0], 0)]\n\tfor i in range(1, n):\n\t\tif arr[i] > maxarr[-1][0]:\n\t\t\tmaxarr.append((arr[i], i))\n\t\telse:\n\t\t\tmaxarr.append(maxarr[-1])\n\tcurr = n - 1\n\tans = []\n\twhile curr > -1:\n\t\tfor i in range(maxarr[curr][1], curr + 1):\n\t\t\tans.append(arr[i])\n\t\tcurr = maxarr[curr][1] - 1\n\tprint(' '.join((str(i) for i in ans)))\n", "def process(cards):\n\tmaxes = [[cards[0]]]\n\tn = len(cards)\n\tfor i in range(1, n):\n\t\tif cards[i] > maxes[-1][0]:\n\t\t\tmaxes.append([cards[i]])\n\t\telse:\n\t\t\tmaxes[-1].append(cards[i])\n\tanswer = []\n\tm = len(maxes)\n\tfor i in range(m):\n\t\tx = maxes[m - 1 - i]\n\t\tfor y in x:\n\t\t\tanswer.append(y)\n\treturn answer\nT = int(input())\nfor I in range(T):\n\tn = int(input())\n\tcards = [int(x) for x in input().split()]\n\tcards = process(cards)\n\tcards = ' '.join(map(str, cards))\n\tprint(cards)\n", "import sys\n\ndef debug(*args):\n\tprint(*args, file=sys.stderr)\n\ndef read_str():\n\treturn sys.stdin.readline().strip()\n\ndef read_int():\n\treturn int(sys.stdin.readline().strip())\n\ndef read_ints():\n\treturn map(int, sys.stdin.readline().strip().split())\n\ndef read_str_split():\n\treturn list(sys.stdin.readline().strip())\n\ndef read_int_list():\n\treturn list(map(int, sys.stdin.readline().strip().split()))\n\ndef Main():\n\tt = read_int()\n\tfor _ in range(t):\n\t\tn = read_int()\n\t\tp = read_int_list()\n\t\tnote = [0] * n\n\t\tfor (i, x) in enumerate(p):\n\t\t\tnote[~-x] = i\n\t\tlast = n\n\t\tans = []\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tif note[i] < last:\n\t\t\t\tfor j in range(note[i], last):\n\t\t\t\t\tans.append(p[j])\n\t\t\t\tlast = note[i]\n\t\tprint(*ans)\nMain()\n", "import math\nfor i in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tm = [l[0]]\n\tk = []\n\tc = l[0]\n\tfor i in range(1, n):\n\t\tif l[i] > c:\n\t\t\tk.append(m)\n\t\t\tm = [l[i]]\n\t\t\tc = l[i]\n\t\telse:\n\t\t\tm.append(l[i])\n\tk.append(m)\n\tfor i in range(len(k) - 1, -1, -1):\n\t\tprint(*k[i], end=' ')\n\tprint()\n", "def cards(arr, l):\n\ts = [0]\n\tmaxsof = arr[0]\n\tfor i in range(1, l):\n\t\tif maxsof < arr[i]:\n\t\t\tmaxsof = arr[i]\n\t\t\ts += [i]\n\ts = s[::-1]\n\tupto = l\n\tt = []\n\tfor i in s:\n\t\tfor j in range(i, upto):\n\t\t\tt += [arr[j]]\n\t\tupto = i\n\tprint(*t)\nn = int(input())\nwhile n > 0:\n\tl = int(input())\n\tc = [int(i) for i in input().split()]\n\tcards(c, l)\n\tn -= 1\n", "for _ in range(int(input())):\n\tn = int(input())\n\tli = list(map(int, input().split()))\n\tb = {}\n\tli1 = []\n\tfor i in range(n):\n\t\tb[li[i]] = i\n\tck = n\n\tfor i in range(n, 0, -1):\n\t\tif b[i] <= ck:\n\t\t\tfor j in range(b[i], ck):\n\t\t\t\tli1.append(li[j])\n\t\t\tck = b[i]\n\tprint(*li1)\n", "T = int(input())\nfor _ in range(T):\n\tN = int(input())\n\tA = list(map(int, input().split()))\n\tans = list()\n\tPosition = {i: None for i in range(1, N + 1)}\n\tfor (i, elt) in enumerate(A):\n\t\tPosition[elt] = i\n\ttodo = N\n\tlast = N\n\twhile todo >= 1:\n\t\ti = Position[todo]\n\t\tans.extend(A[i:last])\n\t\tlast = i\n\t\twhile Position[todo] >= N - len(ans):\n\t\t\ttodo -= 1\n\t\t\tif todo < 1:\n\t\t\t\tbreak\n\tprint(' '.join(map(str, ans)))\n", "for _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tdic = dict()\n\tans = []\n\tmaxi = n\n\tfor i in range(n):\n\t\tdic[arr[i]] = i\n\tfor j in range(n, 0, -1):\n\t\tif dic[j] <= maxi:\n\t\t\tans += arr[dic[j]:maxi]\n\t\t\tmaxi = dic[j]\n\tprint(*ans)\n", "_t = int(input())\nfor t in range(_t):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tpos = [0] * (n + 1)\n\tfor i in range(n):\n\t\tpos[a[i]] = i\n\tres = []\n\tlastpos = n\n\tfor i in range(n, 0, -1):\n\t\tp = pos[i]\n\t\tif p >= lastpos:\n\t\t\tcontinue\n\t\tres += a[p:lastpos]\n\t\tlastpos = p\n\t\tif lastpos == 0:\n\t\t\tbreak\n\tprint(' '.join(map(str, res)))\n", "for i in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tsupport = []\n\tmx = 0\n\tmx1 = n\n\tn1 = n\n\tvisited = set()\n\tfor i in range(len(l) - 1, -1, -1):\n\t\tif l[i] == n1:\n\t\t\tsupport += l[i:mx1]\n\t\t\tmx1 = i\n\t\tvisited.add(l[i])\n\t\twhile n1 in visited:\n\t\t\tn1 -= 1\n\tprint(*support)\n", "test = int(input())\nfor _ in range(test):\n\tnn = int(input())\n\tl = list(map(int, input().split()))\n\tmark = [0] * (nn + 1)\n\ti = nn - 1\n\tj = nn\n\tans = []\n\twhile i >= 0 and j >= 1:\n\t\tk = i\n\t\twhile l[k] != j:\n\t\t\tk -= 1\n\t\tfor index in range(k, i + 1):\n\t\t\tans.append(l[index])\n\t\t\tmark[l[index]] = 1\n\t\ti = k - 1\n\t\twhile mark[j] == 1:\n\t\t\tj -= 1\n\tfor i in ans:\n\t\tprint(i, end=' ')\n\tprint()\n", "def debug():\n\tfor i in range(10):\n\t\tpass\n\ndef mmm(a, b, m):\n\tres = 0\n\ta %= m\n\twhile b:\n\t\tif b & 1:\n\t\t\tres = (res + a) % m\n\t\ta = 2 * a % m\n\t\tb >>= 1\n\treturn res\n\ndef pw(a, b, c):\n\tans = 1\n\ta = a % c\n\tif a == 0:\n\t\treturn 0\n\twhile b > 0:\n\t\tif b & 1:\n\t\t\tans = ans * a % c\n\t\tb = b >> 1\n\t\ta = a * a % c\n\treturn ans\ntry:\n\tfor _ in range(int(input())):\n\t\tn = int(input())\n\t\tarr = [int(i) for i in input().split()]\n\t\tarr2 = sorted(arr, reverse=True)\n\t\tmp = {}\n\t\tfor (i, j) in enumerate(arr):\n\t\t\tmp[j] = i\n\t\tans = []\n\t\tfor i in arr2:\n\t\t\tidx = mp[i]\n\t\t\tptr = idx\n\t\t\twhile ptr < n and arr[ptr] != 0:\n\t\t\t\tans.append(arr[ptr])\n\t\t\t\tarr[ptr] = 0\n\t\t\t\tptr += 1\n\t\tprint(*ans)\nexcept EOFError as e:\n\tprint(e)\n", "__version__ = '0.2'\n__date__ = '2021-03-06'\nimport sys\n\ndef solve(n, p):\n\tanswer = []\n\tpos = [0] * (n + 1)\n\tfor i in range(n):\n\t\tpos[p[i]] = i\n\tcur = n\n\tfor largest in range(n, 0, -1):\n\t\tif pos[largest] >= cur:\n\t\t\tcontinue\n\t\ti = pos[largest]\n\t\tanswer.extend(p[i:cur])\n\t\tcur = i\n\treturn answer\n\ndef main(argv=None):\n\tt = int(input())\n\tfor _ in range(t):\n\t\tn = int(input())\n\t\tp = list(map(int, input().split()))\n\t\tprint(' '.join(map(str, solve(n, p))))\n\treturn 0\nSTATUS = main()\nsys.exit(STATUS)\n", "from pprint import pprint\nimport sys\ninput = sys.stdin.readline\n\ndef do():\n\tfrom heapq import heappop, heappush, heapify\n\tn = int(input())\n\todat = list(map(int, input().split()))\n\tdat = []\n\tfor i in range(n):\n\t\tdat.append((-odat[i], i))\n\theapify(dat)\n\tres = []\n\ttotteru = n\n\twhile len(dat) > 0:\n\t\t(curVal, curInd) = heappop(dat)\n\t\tif totteru <= curInd:\n\t\t\tcontinue\n\t\tfor i in range(curInd, totteru):\n\t\t\tres.append(odat[i])\n\t\ttotteru = curInd\n\tprint(' '.join(list(map(str, res))))\nq = int(input())\nfor _ in range(q):\n\tdo()\n", "for _ in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tb = [0] * (n + 1)\n\tans = []\n\tfor i in range(n):\n\t\tb[a[i]] = i\n\tck = n\n\tfor i in range(n, 0, -1):\n\t\tif b[i] <= ck:\n\t\t\tfor j in range(b[i], ck):\n\t\t\t\tans.append(a[j])\n\t\t\tck = b[i]\n\tprint(*ans)\n", "from sys import stdin, stdout\ninput = stdin.readline\ninf = int(1e+20)\nfor _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tprev = n\n\trem = set()\n\tm = n\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif arr[i] == m:\n\t\t\tans += arr[i:prev]\n\t\t\tfor d in arr[i:prev]:\n\t\t\t\trem.add(d)\n\t\t\tprev = i\n\t\t\twhile m in rem:\n\t\t\t\tm -= 1\n\tprint(' '.join(map(str, ans)))\n", "def segfunc(x, y):\n\treturn max(x, y)\nide_ele = -10 ** 18\n\nclass SegTree:\n\n\tdef __init__(self, init_val, segfunc, ide_ele):\n\t\tn = len(init_val)\n\t\tself.segfunc = segfunc\n\t\tself.ide_ele = ide_ele\n\t\tself.num = 1 << (n - 1).bit_length()\n\t\tself.tree = [ide_ele] * 2 * self.num\n\t\tfor i in range(n):\n\t\t\tself.tree[self.num + i] = init_val[i]\n\t\tfor i in range(self.num - 1, 0, -1):\n\t\t\tself.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])\n\n\tdef update(self, k, x):\n\t\tk += self.num\n\t\tself.tree[k] = x\n\t\twhile k > 1:\n\t\t\tself.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])\n\t\t\tk >>= 1\n\n\tdef query(self, l, r):\n\t\tres = self.ide_ele\n\t\tl += self.num\n\t\tr += self.num\n\t\twhile l < r:\n\t\t\tif l & 1:\n\t\t\t\tres = self.segfunc(res, self.tree[l])\n\t\t\t\tl += 1\n\t\t\tif r & 1:\n\t\t\t\tres = self.segfunc(res, self.tree[r - 1])\n\t\t\tl >>= 1\n\t\t\tr >>= 1\n\t\treturn res\nimport sys\ninput = sys.stdin.readline\nimport math\nimport copy\nt = int(input())\nfor f in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tans = []\n\ta = copy.copy(p)\n\ta.reverse()\n\tlast = n\n\tseg = SegTree(p, segfunc, ide_ele)\n\tflg = seg.query(0, last)\n\tfor i in range(n):\n\t\tif a[i] == flg:\n\t\t\tfor j in range(n - i - 1, last):\n\t\t\t\tans.append(p[j])\n\t\t\tlast = n - i - 1\n\t\t\tflg = seg.query(0, last)\n\tprint(*ans)\n", "import sys\ninput = sys.stdin.readline\nfor _ in range(int(input())):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tq = [(pi, i) for (i, pi) in enumerate(p)]\n\tq.sort()\n\t(pi, i) = q.pop()\n\tnp = p[i:]\n\twhile q:\n\t\t(npi, ni) = q.pop()\n\t\tif ni < i:\n\t\t\tnp += p[ni:i]\n\t\t\ti = ni\n\tprint(*np)\n", "for _ in range(int(input())):\n\tn = int(input())\n\ta = [*map(int, input().split())]\n\tans = []\n\tj = n\n\tfinal = [0] * n\n\tfor i in range(n):\n\t\tfinal[a[i] - 1] = i\n\tfor i in range(n - 1, -1, -1):\n\t\tif final[i] < j:\n\t\t\tans += a[final[i]:j]\n\t\t\tj = final[i]\n\tprint(*ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tli = list(map(int, input().split()))\n\tindex = [0] * n\n\tfor i in range(n):\n\t\tindex[li[i] - 1] = i\n\ttemp = n\n\tans = []\n\tfor ind in reversed(index):\n\t\tif ind < temp:\n\t\t\tans += li[ind:temp]\n\t\t\ttemp = ind\n\tprint(*ans)\n", "import sys\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfor _ in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tb = []\n\td = dict()\n\tt = 0\n\tck = []\n\tfor (i, v) in enumerate(a):\n\t\td[v] = i\n\t\tt = max(t, v)\n\t\tck.append(t)\n\tl = n\n\twhile len(b) != n:\n\t\tmv = ck[l - 1]\n\t\tfor i in range(d[mv], l):\n\t\t\tb.append(a[i])\n\t\tl = d[mv]\n\tprint(*b)\n", "import os, sys, heapq as h, time\nfrom io import BytesIO, IOBase\nfrom types import GeneratorType\nfrom bisect import bisect_left, bisect_right\nfrom collections import defaultdict as dd, deque as dq, Counter as dc\nimport math, string\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\timport os\n\t\tself.os = os\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tself.os.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef getInts():\n\treturn [int(s) for s in input().split()]\n\ndef getInt():\n\treturn int(input())\n\ndef getStrs():\n\treturn [s for s in input().split()]\n\ndef getStr():\n\treturn input()\n\ndef listStr():\n\treturn list(input())\n\ndef getMat(n):\n\treturn [getInts() for _ in range(n)]\n\ndef isInt(s):\n\treturn '0' <= s[0] <= '9'\nMOD = 10 ** 9 + 7\n\nclass SortedList:\n\n\tdef __init__(self, iterable=[], _load=200):\n\t\tvalues = sorted(iterable)\n\t\tself._len = _len = len(values)\n\t\tself._load = _load\n\t\tself._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)]\n\t\tself._list_lens = [len(_list) for _list in _lists]\n\t\tself._mins = [_list[0] for _list in _lists]\n\t\tself._fen_tree = []\n\t\tself._rebuild = True\n\n\tdef _fen_build(self):\n\t\tself._fen_tree[:] = self._list_lens\n\t\t_fen_tree = self._fen_tree\n\t\tfor i in range(len(_fen_tree)):\n\t\t\tif i | i + 1 < len(_fen_tree):\n\t\t\t\t_fen_tree[i | i + 1] += _fen_tree[i]\n\t\tself._rebuild = False\n\n\tdef _fen_update(self, index, value):\n\t\tif not self._rebuild:\n\t\t\t_fen_tree = self._fen_tree\n\t\t\twhile index < len(_fen_tree):\n\t\t\t\t_fen_tree[index] += value\n\t\t\t\tindex |= index + 1\n\n\tdef _fen_query(self, end):\n\t\tif self._rebuild:\n\t\t\tself._fen_build()\n\t\t_fen_tree = self._fen_tree\n\t\tx = 0\n\t\twhile end:\n\t\t\tx += _fen_tree[end - 1]\n\t\t\tend &= end - 1\n\t\treturn x\n\n\tdef _fen_findkth(self, k):\n\t\t_list_lens = self._list_lens\n\t\tif k < _list_lens[0]:\n\t\t\treturn (0, k)\n\t\tif k >= self._len - _list_lens[-1]:\n\t\t\treturn (len(_list_lens) - 1, k + _list_lens[-1] - self._len)\n\t\tif self._rebuild:\n\t\t\tself._fen_build()\n\t\t_fen_tree = self._fen_tree\n\t\tidx = -1\n\t\tfor d in reversed(range(len(_fen_tree).bit_length())):\n\t\t\tright_idx = idx + (1 << d)\n\t\t\tif right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]:\n\t\t\t\tidx = right_idx\n\t\t\t\tk -= _fen_tree[idx]\n\t\treturn (idx + 1, k)\n\n\tdef _delete(self, pos, idx):\n\t\t_lists = self._lists\n\t\t_mins = self._mins\n\t\t_list_lens = self._list_lens\n\t\tself._len -= 1\n\t\tself._fen_update(pos, -1)\n\t\tdel _lists[pos][idx]\n\t\t_list_lens[pos] -= 1\n\t\tif _list_lens[pos]:\n\t\t\t_mins[pos] = _lists[pos][0]\n\t\telse:\n\t\t\tdel _lists[pos]\n\t\t\tdel _list_lens[pos]\n\t\t\tdel _mins[pos]\n\t\t\tself._rebuild = True\n\n\tdef _loc_left(self, value):\n\t\tif not self._len:\n\t\t\treturn (0, 0)\n\t\t_lists = self._lists\n\t\t_mins = self._mins\n\t\t(lo, pos) = (-1, len(_lists) - 1)\n\t\twhile lo + 1 < pos:\n\t\t\tmi = lo + pos >> 1\n\t\t\tif value <= _mins[mi]:\n\t\t\t\tpos = mi\n\t\t\telse:\n\t\t\t\tlo = mi\n\t\tif pos and value <= _lists[pos - 1][-1]:\n\t\t\tpos -= 1\n\t\t_list = _lists[pos]\n\t\t(lo, idx) = (-1, len(_list))\n\t\twhile lo + 1 < idx:\n\t\t\tmi = lo + idx >> 1\n\t\t\tif value <= _list[mi]:\n\t\t\t\tidx = mi\n\t\t\telse:\n\t\t\t\tlo = mi\n\t\treturn (pos, idx)\n\n\tdef _loc_right(self, value):\n\t\tif not self._len:\n\t\t\treturn (0, 0)\n\t\t_lists = self._lists\n\t\t_mins = self._mins\n\t\t(pos, hi) = (0, len(_lists))\n\t\twhile pos + 1 < hi:\n\t\t\tmi = pos + hi >> 1\n\t\t\tif value < _mins[mi]:\n\t\t\t\thi = mi\n\t\t\telse:\n\t\t\t\tpos = mi\n\t\t_list = _lists[pos]\n\t\t(lo, idx) = (-1, len(_list))\n\t\twhile lo + 1 < idx:\n\t\t\tmi = lo + idx >> 1\n\t\t\tif value < _list[mi]:\n\t\t\t\tidx = mi\n\t\t\telse:\n\t\t\t\tlo = mi\n\t\treturn (pos, idx)\n\n\tdef add(self, value):\n\t\t_load = self._load\n\t\t_lists = self._lists\n\t\t_mins = self._mins\n\t\t_list_lens = self._list_lens\n\t\tself._len += 1\n\t\tif _lists:\n\t\t\t(pos, idx) = self._loc_right(value)\n\t\t\tself._fen_update(pos, 1)\n\t\t\t_list = _lists[pos]\n\t\t\t_list.insert(idx, value)\n\t\t\t_list_lens[pos] += 1\n\t\t\t_mins[pos] = _list[0]\n\t\t\tif _load + _load < len(_list):\n\t\t\t\t_lists.insert(pos + 1, _list[_load:])\n\t\t\t\t_list_lens.insert(pos + 1, len(_list) - _load)\n\t\t\t\t_mins.insert(pos + 1, _list[_load])\n\t\t\t\t_list_lens[pos] = _load\n\t\t\t\tdel _list[_load:]\n\t\t\t\tself._rebuild = True\n\t\telse:\n\t\t\t_lists.append([value])\n\t\t\t_mins.append(value)\n\t\t\t_list_lens.append(1)\n\t\t\tself._rebuild = True\n\n\tdef discard(self, value):\n\t\t_lists = self._lists\n\t\tif _lists:\n\t\t\t(pos, idx) = self._loc_right(value)\n\t\t\tif idx and _lists[pos][idx - 1] == value:\n\t\t\t\tself._delete(pos, idx - 1)\n\n\tdef remove(self, value):\n\t\t_len = self._len\n\t\tself.discard(value)\n\t\tif _len == self._len:\n\t\t\traise ValueError('{0!r} not in list'.format(value))\n\n\tdef pop(self, index=-1):\n\t\t(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)\n\t\tvalue = self._lists[pos][idx]\n\t\tself._delete(pos, idx)\n\t\treturn value\n\n\tdef bisect_left(self, value):\n\t\t(pos, idx) = self._loc_left(value)\n\t\treturn self._fen_query(pos) + idx\n\n\tdef bisect_right(self, value):\n\t\t(pos, idx) = self._loc_right(value)\n\t\treturn self._fen_query(pos) + idx\n\n\tdef count(self, value):\n\t\treturn self.bisect_right(value) - self.bisect_left(value)\n\n\tdef __len__(self):\n\t\treturn self._len\n\n\tdef __getitem__(self, index):\n\t\t(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)\n\t\treturn self._lists[pos][idx]\n\n\tdef __delitem__(self, index):\n\t\t(pos, idx) = self._fen_findkth(self._len + index if index < 0 else index)\n\t\tself._delete(pos, idx)\n\n\tdef __contains__(self, value):\n\t\t_lists = self._lists\n\t\tif _lists:\n\t\t\t(pos, idx) = self._loc_left(value)\n\t\t\treturn idx < len(_lists[pos]) and _lists[pos][idx] == value\n\t\treturn False\n\n\tdef __iter__(self):\n\t\treturn (value for _list in self._lists for value in _list)\n\n\tdef __reversed__(self):\n\t\treturn (value for _list in reversed(self._lists) for value in reversed(_list))\n\n\tdef __repr__(self):\n\t\treturn 'SortedList({0})'.format(list(self))\n\ndef solve():\n\tN = getInt()\n\tA = getInts()\n\tans = []\n\tB = SortedList(A)\n\twhile B:\n\t\tidx = len(A) - 1\n\t\tm = B[-1]\n\t\twhile A[idx] != m:\n\t\t\tidx -= 1\n\t\tfor j in range(idx, len(A)):\n\t\t\tans.append(A[j])\n\t\t\tB.remove(A[j])\n\t\tfor j in range(idx, len(A)):\n\t\t\tA.pop()\n\tprint(*ans)\n\treturn\nfor _ in range(getInt()):\n\tsolve()\n", "from sys import *\ninput = lambda : stdin.readline()\nint_arr = lambda : list(map(int, stdin.readline().strip().split()))\nfor _ in range(int(input())):\n\tn = int(input())\n\tarr = int_arr()\n\tind = [0] * n\n\tfor i in range(n):\n\t\tind[n - arr[i]] = i\n\tlast = n\n\tres = []\n\tfor i in range(n):\n\t\tif ind[i] <= last:\n\t\t\tres += arr[ind[i]:last]\n\t\t\tlast = ind[i]\n\tprint(*res)\n", "import itertools\nfor _ in range(int(input())):\n\tn = int(input())\n\tp = list(map(int, input().split(' ')))\n\tcands = []\n\tnew_deck = []\n\tfor pi in p:\n\t\tif not cands:\n\t\t\tcands.append(pi)\n\t\t\tord_cands = pi\n\t\telif pi < cands[0]:\n\t\t\tcands.append(pi)\n\t\telse:\n\t\t\tnew_deck.append(cands)\n\t\t\tcands = [pi]\n\tnew_deck.append(cands)\n\tprint(*itertools.chain.from_iterable(new_deck[::-1]))\n", "from collections import deque\n\ndef solve(arr):\n\tsorted_arr = sorted([(val, idx) for (idx, val) in enumerate(arr)], reverse=True)\n\tN = len(arr)\n\tcurr_idx = N - 1\n\toutput = []\n\ti = 0\n\twhile curr_idx >= 0:\n\t\t(val, new_idx) = sorted_arr[i]\n\t\tif curr_idx < new_idx:\n\t\t\ti += 1\n\t\t\tcontinue\n\t\tfor j in range(new_idx, curr_idx + 1):\n\t\t\toutput.append(str(arr[j]))\n\t\ti += 1\n\t\tcurr_idx = new_idx - 1\n\tprint(' '.join(output))\n\treturn\nt = int(input())\nwhile t:\n\tt -= 1\n\tn = int(input())\n\tarr = [int(val) for val in input().split(' ')]\n\tsolve(arr)\n", "import sys\nfrom collections import *\nimport math\nimport bisect\n\ndef input():\n\treturn sys.stdin.readline()\nfor _ in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tb = []\n\tc = -1\n\tc1 = []\n\tfor i in range(n):\n\t\tif a[i] > c:\n\t\t\tb.append(i)\n\t\t\tc = a[i]\n\tfor i in range(len(b) - 1, -1, -1):\n\t\tif len(c1) == 0:\n\t\t\tc1.extend(a[b[i]:])\n\t\telse:\n\t\t\tc1.extend(a[b[i]:b[i + 1]])\n\tprint(*c1)\n", "from collections import defaultdict\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\ta.reverse()\n\tcheck = n\n\tans = []\n\ttmp = []\n\td = defaultdict(int)\n\tfor i in range(n):\n\t\td[a[i]] += 1\n\t\tif a[i] == check:\n\t\t\ttmp.append(a[i])\n\t\t\ttmp.reverse()\n\t\t\tans.append(tmp)\n\t\t\ttmp = []\n\t\t\twhile True:\n\t\t\t\tif d[check] == 1:\n\t\t\t\t\tcheck -= 1\n\t\t\t\telse:\n\t\t\t\t\tbreak\n\t\t\t\tif check == 1:\n\t\t\t\t\tbreak\n\t\telse:\n\t\t\ttmp.append(a[i])\n\tans2 = []\n\tfor ary in ans:\n\t\tfor i in ary:\n\t\t\tans2.append(i)\n\tprint(*ans2)\n", "mod = 1000000007\neps = 10 ** (-9)\n\ndef main():\n\timport sys\n\tinput = sys.stdin.readline\n\tfor _ in range(int(input())):\n\t\tN = int(input())\n\t\tP = list(map(int, input().split()))\n\t\tma = [0] * N\n\t\tma[0] = P[0]\n\t\tfor i in range(1, N):\n\t\t\tma[i] = max(ma[i - 1], P[i])\n\t\tans = []\n\t\ttmp = []\n\t\tm = N\n\t\tfor i in range(N - 1, -1, -1):\n\t\t\tp = P[i]\n\t\t\ttmp.append(p)\n\t\t\tif p == m:\n\t\t\t\ttmp.reverse()\n\t\t\t\tans.extend(tmp)\n\t\t\t\ttmp = []\n\t\t\t\tm = ma[i - 1]\n\t\tprint(*ans)\nmain()\n", "for _ in range(int(input())):\n\tn = int(input())\n\ta = [int(x) for x in input().split()]\n\t(ind, ce, ans) = ([0] * n, n, [])\n\tfor i in range(n):\n\t\tind[n - a[i]] = i\n\tfor i in ind:\n\t\tif i < ce:\n\t\t\tans += a[i:ce]\n\t\t\tce = i\n\tprint(*ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tp = [int(x) for x in input().split()]\n\tpref = [0] * n\n\tpref[0] = p[0]\n\tfor i in range(1, n):\n\t\tpref[i] = max(p[i], pref[i - 1])\n\tpp = [0] * n\n\ti = n - 1\n\tx = n\n\tprev = n\n\tstart = 0\n\twhile i >= 0:\n\t\twhile i >= 0 and p[i] != pref[i]:\n\t\t\ti -= 1\n\t\tfor j in range(max(0, i), prev):\n\t\t\tpp[start] = p[j]\n\t\t\tstart += 1\n\t\tprev = i\n\t\ti -= 1\n\tprint(*pp)\n", "import copy\nt = int(input())\nnp = []\nans = []\nfor _ in range(t):\n\tnp.append([])\n\tn = int(input())\n\tnp[-1].append(n)\n\tp = list(map(int, input().split()))\n\tnp[-1].append(p)\nfor i in range(t):\n\tn = np[i][0]\n\tp = np[i][1]\n\tans.append([])\n\tindex = [0 for _ in range(n)]\n\tfor j in range(n):\n\t\tindex[p[j] - 1] = j\n\tbiggest_index = n\n\tfor j in index[::-1]:\n\t\tif j < biggest_index:\n\t\t\tans[-1] += p[j:biggest_index]\n\t\t\tbiggest_index = j\n\ndef print_list(lst):\n\tfor i in range(len(lst)):\n\t\tif i != len(lst) - 1:\n\t\t\tprint(lst[i], end=' ')\n\t\telse:\n\t\t\tprint(lst[i])\nfor elem in ans:\n\tprint_list(elem)\n", "t = int(input())\nfor cs in range(t):\n\tn = int(input())\n\tp = [int(s) for s in input().split()]\n\tans = 0\n\tpp = []\n\tmp = [True] * (n + 1)\n\tcurrmax = n\n\tii = n\n\tfor i in range(n - 1, -1, -1):\n\t\tmp[p[i]] = False\n\t\tif p[i] == currmax:\n\t\t\tpp += p[i:ii]\n\t\t\tii = i\n\t\t\twhile mp[currmax] == False:\n\t\t\t\tcurrmax -= 1\n\tprint(*pp)\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tmax = 0\n\tl = []\n\tfor (i, v) in enumerate(p):\n\t\tif max < v:\n\t\t\tl.append(i)\n\t\t\tmax = v\n\tans = []\n\tf = n\n\tfor i in l[::-1]:\n\t\tans += p[i:f]\n\t\tf = i\n\tprint(*ans, sep=' ')\n", "T = int(input())\nfor t in range(T):\n\tN = int(input())\n\tP = list(map(int, input().split()))\n\tD = [-1 for n in range(N + 1)]\n\tS = [-1 for n in range(N + 1)]\n\tfor x in range(N):\n\t\tD[P[x]] = x\n\ts = N\n\tR = []\n\tstop = N\n\twhile s > 0:\n\t\tif S[s] == -1:\n\t\t\tstart = D[s]\n\t\t\tfor x in range(start, stop):\n\t\t\t\tR.append(P[x])\n\t\t\t\tS[P[x]] = 1\n\t\t\tstop = start\n\t\ts = s - 1\n\tprint(*R)\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tmaxi = []\n\tcurr = -1\n\tpos = -1\n\tfor i in range(n):\n\t\tif arr[i] > curr:\n\t\t\tcurr = arr[i]\n\t\t\tpos = i\n\t\tmaxi.append([curr, pos])\n\tans = []\n\tpos = n - 1\n\twhile pos >= 0:\n\t\tt = maxi[pos][1]\n\t\tfor i in range(t, pos + 1):\n\t\t\tans.append(arr[i])\n\t\tpos = t - 1\n\tfor i in ans:\n\t\tprint(i, end=' ')\n\tprint()\n", "for t in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\t(q, z, r) = ([0] * n, n, [])\n\tfor i in range(n):\n\t\tq[n - a[i]] = i\n\tfor i in q:\n\t\tif i < z:\n\t\t\tr += a[i:z]\n\t\t\tz = i\n\tprint(*r)\n", "def main():\n\tt = int(input())\n\tfor _ in range(t):\n\t\tn = int(input())\n\t\tp = list(map(int, input().split()))\n\t\ttaken = [0] * (len(p) + 1)\n\t\tn_max = n\n\t\tlast_p = n\n\t\tdeck = []\n\t\tfor i in range(n):\n\t\t\tif p[n - i - 1] == n_max:\n\t\t\t\tfor j in range(n - i - 1, last_p):\n\t\t\t\t\tdeck.append(p[j])\n\t\t\t\tlast_p = n - i - 1\n\t\t\t\tfor j in range(n_max - 1, 0, -1):\n\t\t\t\t\tif taken[j] == 0:\n\t\t\t\t\t\tn_max = j\n\t\t\t\t\t\tbreak\n\t\t\ttaken[p[n - i - 1]] = 1\n\t\ts = ''\n\t\tfor d in deck:\n\t\t\ts += str(d) + ' '\n\t\tprint(s)\nmain()\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tind = [0]\n\tv = l[0]\n\tfor i in range(1, n):\n\t\tif l[i] > v:\n\t\t\tv = l[i]\n\t\t\tind.append(i)\n\tind = ind[::-1]\n\ten = n\n\tans = []\n\tfor i in ind:\n\t\tfor j in range(i, en):\n\t\t\tans.append(l[j])\n\t\ten = i\n\tprint(*ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tseq = list(map(int, input().split()))\n\tlocmax = [0]\n\tans = []\n\tfor i in range(1, n):\n\t\tif seq[i] > seq[locmax[-1]]:\n\t\t\tlocmax.append(i)\n\tlocmax = [n] + locmax[::-1]\n\tfor el in range(len(locmax) - 1):\n\t\tprint(*seq[locmax[el + 1]:locmax[el]], end=' ')\n\tprint()\n", "for i in range(int(input())):\n\tinput()\n\tbaralho = list(map(int, input().split(' ')))\n\tbaralho_ordenado = len(baralho) * [0]\n\tfor (indice, carta) in enumerate(baralho):\n\t\tbaralho_ordenado[carta - 1] = indice\n\tnovo_baralho = []\n\twhile baralho:\n\t\tindice_maior_carta = baralho_ordenado.pop()\n\t\tif indice_maior_carta < len(baralho):\n\t\t\tnovo_baralho += baralho[indice_maior_carta:]\n\t\t\tdel baralho[indice_maior_carta:]\n\tprint(' '.join(map(str, novo_baralho)))\n", "T = int(input())\nfor _ in range(T):\n\tn = int(input())\n\tA = list(map(int, input().split()))\n\tA.reverse()\n\tans = []\n\ttemp = []\n\tma = -1\n\tfor i2 in range(0, n):\n\t\ti = n - i2 - 1\n\t\tif A[i] > ma:\n\t\t\tma = A[i]\n\t\t\ttemp.reverse()\n\t\t\tfor t in temp:\n\t\t\t\tans.append(t)\n\t\t\ttemp = []\n\t\ttemp.append(A[i])\n\ttemp.reverse()\n\tfor t in temp:\n\t\tans.append(t)\n\tans.reverse()\n\tprint(*ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tlis = list(map(int, input().split()))\n\tlook = [0 for i in range(n)]\n\tlook[0] = lis[0]\n\tfor i in range(1, n):\n\t\tlook[i] = max(look[i - 1], lis[i])\n\tj = n\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif look[i] == lis[i]:\n\t\t\tans.extend(lis[i:j])\n\t\t\tj = i\n\tprint(*ans)\n", "def fmax(m, n):\n\tp = [0 for i in range(n)]\n\tfor i in range(len(m)):\n\t\tp[n - m[i]] = i\n\treturn p\nfor _ in range(int(input())):\n\tn = int(input())\n\tm = list(map(int, input().split()))\n\tp = fmax(m, n)\n\tindex = n\n\tfor i in p:\n\t\tif index > i:\n\t\t\tprint(*m[i:index], end=' ', sep=' ')\n\t\t\tindex = i\n\tprint()\n", "for i in range(int(input())):\n\tn = int(input())\n\tdeck_list = list(map(int, input().split()))\n\tmax_list = []\n\tstack = []\n\tmax = -1\n\tfor card in deck_list:\n\t\tif card > max:\n\t\t\tmax = card\n\t\tmax_list.append(max)\n\tfor i in range(len(deck_list) - 1, -1, -1):\n\t\tif deck_list[i] < max_list[i]:\n\t\t\tstack.append(deck_list[i])\n\t\telse:\n\t\t\tprint(deck_list[i], end=' ')\n\t\t\twhile len(stack) > 0:\n\t\t\t\tprint(stack.pop(), end=' ')\n\tprint()\n", "t = int(input())\nwhile t > 0:\n\tn = input()\n\tsz = int(n)\n\tarr = input().split(' ')\n\tarr = [int(x) for x in arr]\n\tpr = [0] * sz\n\ttemp = []\n\tans = []\n\tfor i in range(0, int(n)):\n\t\tif arr[int(n) - 1 - i] == sz:\n\t\t\ttemp.append(arr[int(n) - 1 - i])\n\t\t\tfor j in range(0, len(temp)):\n\t\t\t\tans.append(temp[len(temp) - 1 - j])\n\t\t\tpr[sz - 1] = 1\n\t\t\ttemp = []\n\t\t\twhile pr[sz - 1] == 1 and sz > 0:\n\t\t\t\tsz = sz - 1\n\t\telse:\n\t\t\tpr[arr[int(n) - 1 - i] - 1] = 1\n\t\t\ttemp.append(arr[int(n) - 1 - i])\n\tfor i in ans:\n\t\tprint(i, end=' ')\n\tprint('')\n\tt = t - 1\n", "def solve():\n\tn = int(input())\n\tp = [int(x) for x in input().split()]\n\tvisited = [False] * (n + 1)\n\tres = []\n\tfor i in range(n, 0, -1):\n\t\tif not visited[i]:\n\t\t\ttem = []\n\t\t\twhile p and p[-1] != i:\n\t\t\t\tval = p.pop()\n\t\t\t\tvisited[val] = True\n\t\t\t\ttem.append(val)\n\t\t\ttem.append(p.pop())\n\t\t\tres += tem[::-1]\n\tprint(*res)\nfor _ in range(int(input())):\n\tsolve()\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\td = dict()\n\tfor i in range(n):\n\t\td[l[i]] = i\n\tans = []\n\tprev = n\n\tfor i in range(n, 0, -1):\n\t\tif d[i] <= prev:\n\t\t\tans += l[d[i]:prev]\n\t\t\tprev = d[i]\n\tprint(*ans)\n", "def fun(ls, var):\n\tdct = {}\n\tfor (i, val) in enumerate(ls):\n\t\tdct[val] = i\n\tst = sorted(ls)\n\tlast_pop_index = var\n\tans = []\n\tfor i in st[::-1]:\n\t\tget_index = dct.get(i)\n\t\tif get_index < last_pop_index:\n\t\t\tfor j in range(get_index, last_pop_index):\n\t\t\t\tans.append(ls[j])\n\t\t\tlast_pop_index = get_index\n\tprint(*ans)\nT = int(input())\nfor i in range(T):\n\tv = int(input())\n\tls = list(map(int, input().split()))\n\tfun(ls, v)\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tA = [0] * (n + 1)\n\ti = 0\n\twhile i < n:\n\t\tA[p[i]] = i\n\t\ti += 1\n\ti = n\n\tLj = n + 1\n\tanswer = ''\n\tL = n\n\twhile i > 0:\n\t\tj = A[i]\n\t\tif j >= Lj:\n\t\t\ti -= 1\n\t\telse:\n\t\t\tLj = j\n\t\t\tl = j\n\t\t\twhile j < L:\n\t\t\t\tanswer += str(p[j]) + ' '\n\t\t\t\tj += 1\n\t\t\tL = l\n\t\t\ti -= 1\n\tprint(answer)\n", "def solution():\n\tt = int(input())\n\tfor _ in range(t):\n\t\tn = int(input())\n\t\tdeck = list(map(int, input().split()))\n\t\tdeck.reverse()\n\t\tmarked = (n + 1) * [False]\n\t\tans = []\n\t\ti = 0\n\t\tfor x in range(n, 0, -1):\n\t\t\tif not marked[x]:\n\t\t\t\told_i = i\n\t\t\t\twhile deck[i] != x:\n\t\t\t\t\tmarked[deck[i]] = True\n\t\t\t\t\ti += 1\n\t\t\t\tmarked[x] = True\n\t\t\t\ti += 1\n\t\t\t\tans.extend(reversed(deck[old_i:i]))\n\t\tprint(*ans)\nsolution()\n", "for _ in range(int(input())):\n\tn = int(input())\n\ta = [*map(int, input().split())]\n\t(ans, t) = ([], n)\n\tind = [0] * n\n\tfor i in range(n):\n\t\tind[a[i] - 1] = i\n\tfor i in ind[::-1]:\n\t\tif i < t:\n\t\t\tans += a[i:t]\n\t\t\tt = i\n\tprint(*ans)\n", "import sys\ninput = sys.stdin.readline\n\ndef solve():\n\tn = int(input())\n\tp = [0] * (n + 1)\n\ta = list(map(int, input().split()))\n\tfor i in range(n):\n\t\tp[a[i]] = i\n\tprev = n\n\tres = []\n\tfor i in range(n, 0, -1):\n\t\tif p[i] < prev:\n\t\t\tfor j in range(p[i], prev):\n\t\t\t\tres.append(a[j])\n\t\t\tprev = p[i]\n\tprint(' '.join(map(str, res)))\nfor i in range(int(input())):\n\tsolve()\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline()\n\ndef RL():\n\treturn map(int, sys.stdin.readline().split())\n\ndef RLL():\n\treturn list(map(int, sys.stdin.readline().split()))\n\ndef N():\n\treturn int(input())\n\ndef S():\n\treturn input().strip()\n\ndef print_list(l):\n\tprint(' '.join(map(str, l)))\nfor _ in range(N()):\n\tn = N()\n\ta = RLL()\n\tnow = 1\n\tm = a[0]\n\tt = []\n\tfor v in a[1:]:\n\t\tif v > m:\n\t\t\tt.append(now)\n\t\t\t(now, m) = (1, v)\n\t\telse:\n\t\t\tnow += 1\n\tt.append(now)\n\tans = []\n\tr = n\n\tfor v in t[::-1]:\n\t\tfor i in range(r - v, r):\n\t\t\tans.append(a[i])\n\t\tr -= v\n\tprint_list(ans)\n", "def solve():\n\tn = int(input())\n\ta = [int(x) for x in input().split()]\n\tdic = {}\n\tfor j in range(n):\n\t\tdic[a[j]] = j\n\tma = n\n\ttemp = []\n\tfor j in range(n, 0, -1):\n\t\tif dic[j] < ma:\n\t\t\tfor k in range(dic[j], ma):\n\t\t\t\ttemp.append(a[k])\n\t\t\tma = dic[j]\n\tprint(*temp)\nfor _ in range(int(input())):\n\tsolve()\n", "import sys\nn = int(input())\nfor _ in range(n):\n\tnn = int(input())\n\tcards = list(map(int, sys.stdin.readline().strip().split()))\n\tmax_ = cards[0]\n\tli = [max_]\n\tfor i in cards[1:]:\n\t\tmax_ = max(max_, i)\n\t\tli.append(max_)\n\tali = []\n\tans = []\n\tpre = None\n\tfor (i, v) in enumerate(li[::-1]):\n\t\ti = nn - i - 1\n\t\tif not (pre == None or pre == v):\n\t\t\tans.extend(ali[::-1])\n\t\t\tali = []\n\t\tali.append(cards[i])\n\t\tpre = v\n\tans.extend(ali[::-1])\n\tprint(' '.join(map(str, ans)))\n", "t = int(input())\nr = []\nfor _ in range(t):\n\tn = int(input())\n\tnums = list(input().split())\n\tpos = [0] * n\n\tfor i in range(n):\n\t\tpos[int(nums[i]) - 1] = i\n\tindex = 0\n\tend = n - 1\n\ts = ''\n\tfor i in range(n - 1, -1, -1):\n\t\tif pos[i] > end:\n\t\t\tcontinue\n\t\tfor j in nums[pos[i]:end + 1]:\n\t\t\tprint(j, end=' ')\n\t\tend = pos[i] - 1\n", "def PROBLEM():\n\tfor _ in range(int(input())):\n\t\tn = int(input())\n\t\tP = list(map(int, input().split()))\n\t\tA = [0] * n\n\t\tA[0] = P[0]\n\t\tfor i in range(1, n):\n\t\t\tA[i] = max(P[i], A[i - 1])\n\t\tj = n\n\t\tT = []\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tif P[i] == A[i]:\n\t\t\t\tT.extend(P[i:j])\n\t\t\t\tj = i\n\t\tprint(*T)\nPROBLEM()\n", "import math\nimport collections\n\ndef read_list() -> list:\n\treturn [int(i) for i in input().strip().split()]\n\ndef read_num() -> int:\n\treturn int(input().strip())\nt = read_num()\nfor _ in range(t):\n\tn = read_num()\n\tinit = read_list()\n\tans = []\n\tcnt = n - 1\n\tmark = [1]\n\ttmp = init[0]\n\tfor i in range(1, n):\n\t\tif init[i] > tmp:\n\t\t\ttmp = init[i]\n\t\t\tmark.append(1)\n\t\telse:\n\t\t\tmark.append(0)\n\tlst = n\n\tfor cnt in range(n - 1, -1, -1):\n\t\tif mark[cnt] == 1:\n\t\t\tans += init[cnt:lst]\n\t\t\tlst = cnt\n\t\tcnt -= 1\n\tfor i in ans:\n\t\tprint(i, end=' ')\n\tprint()\n", "for _ in range(int(input())):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tq = []\n\tind = []\n\tmi = -1\n\tfor i in range(n):\n\t\tif p[i] > mi:\n\t\t\tind.append(i)\n\t\t\tmi = p[i]\n\tind.append(n)\n\tn = len(ind)\n\tfor i in range(n - 1):\n\t\tq += p[ind[n - 2 - i]:ind[n - 1 - i]]\n\tprint(' '.join([str(o) for o in q]))\n", "t = int(input())\nfor i in range(t):\n\ta = int(input())\n\tl = input().split(' ')\n\tm = []\n\tma = 0\n\tfor j in range(a):\n\t\tl[j] = int(l[j])\n\t\tif l[j] > l[ma]:\n\t\t\tma = j\n\t\tm.append(ma)\n\tj = a - 1\n\twhile j >= 0:\n\t\ty = j\n\t\tj = m[j]\n\t\tfor k in range(j, y + 1):\n\t\t\tprint(l[k], end=' ')\n\t\tj -= 1\n\tprint()\n", "from sys import stdin, exit\nfrom bisect import bisect_left as bl, bisect_right as br\nfrom itertools import accumulate\nyes = lambda : print('YES')\nno = lambda : print('NO')\ninput = lambda : stdin.readline()[:-1]\nintput = lambda : int(input())\nsinput = lambda : input().split()\nintsput = lambda : map(int, sinput())\n\ndef dprint(*args, **kwargs):\n\tif debugging:\n\t\tprint(*args, **kwargs)\ndebugging = 1\nt = intput()\nfor _ in range(t):\n\tn = intput()\n\tp = list(intsput())\n\tindexes = {}\n\tfor (i, x) in enumerate(p):\n\t\tindexes[x] = i\n\tans = []\n\ttop = n - 1\n\ttarget = n\n\twhile top != -1:\n\t\tloc = indexes[target]\n\t\tif loc <= top:\n\t\t\tans += p[loc:top + 1]\n\t\t\ttop = loc - 1\n\t\ttarget -= 1\n\tprint(' '.join(map(str, ans)))\n", "for _ in range(int(input())):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tst = []\n\tprev = n\n\tpos = {}\n\tfor i in range(n):\n\t\tpos.setdefault(p[i], i)\n\tfor i in range(n, 0, -1):\n\t\tif pos[i] <= prev:\n\t\t\tst += p[pos[i]:prev]\n\t\t\tprev = pos[i]\n\tprint(*st)\n", "def solve(P, n):\n\tmoved = [False] * (n + 1)\n\tnextMax = n\n\tnewDeck = []\n\twhile len(P):\n\t\twhile moved[nextMax]:\n\t\t\tnextMax -= 1\n\t\tstack = []\n\t\twhile len(P) and P[-1] != nextMax:\n\t\t\tstack.append(P.pop())\n\t\tstack.append(P.pop())\n\t\tl = len(stack)\n\t\tfor i in range(l):\n\t\t\tnewDeck.append(stack.pop())\n\t\t\tmoved[newDeck[-1]] = True\n\treturn newDeck\nt = int(input())\nfor tc in range(t):\n\tn = int(input())\n\tP = list(map(int, input().split()))\n\tresult = solve(P, n)\n\tprint(*result)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().strip().split()))\n\tnew = [0] * (n + 1)\n\tpoint = n\n\tlis = []\n\tfor x in range(n - 1, -1, -1):\n\t\tif arr[x] == point:\n\t\t\tlis.append(arr[x])\n\t\t\tnew[arr[x]] = 1\n\t\t\tprint(*lis[::-1], end=' ')\n\t\t\tlis = []\n\t\t\tfor y in range(point, 0, -1):\n\t\t\t\tif new[y] == 0:\n\t\t\t\t\tpoint = y\n\t\t\t\t\tbreak\n\t\telse:\n\t\t\tnew[arr[x]] = 1\n\t\t\tlis.append(arr[x])\n", "for _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tnew = []\n\tarr_sort = [i for i in arr]\n\tarr_sort.sort(reverse=True)\n\tmemo = {arr[i]: i for i in range(n)}\n\tmax_index = n\n\tindices = []\n\tfor j in range(n):\n\t\ti = memo[arr_sort[j]]\n\t\tif i < max_index:\n\t\t\tindices.append(i)\n\t\t\tmax_index = i\n\tmax_index = n - 1\n\tfor i in indices:\n\t\tj = i\n\t\twhile j <= max_index:\n\t\t\tnew.append(arr[j])\n\t\t\tj += 1\n\t\tmax_index = i - 1\n\tans = map(str, new)\n\tprint(' '.join(ans))\n", "from collections import defaultdict\nfor _ in range(int(input())):\n\tn = int(input())\n\tpi = list(map(int, input().split()))\n\tdp = [0] * n\n\tcurr = n\n\tright = n\n\tval = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif pi[i] != curr:\n\t\t\tdp[pi[i] - 1] = 1\n\t\telse:\n\t\t\tdp[pi[i] - 1] = 1\n\t\t\tval.append(pi[i:right])\n\t\t\tright = i\n\t\t\tfor ii in range(curr, -1, -1):\n\t\t\t\tif not dp[ii - 1]:\n\t\t\t\t\tcurr = ii\n\t\t\t\t\tbreak\n\tans = []\n\tfor i in val:\n\t\tfor ii in i:\n\t\t\tans.append(ii)\n\tprint(*ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tlst = list(map(int, input().split()))\n\ta = []\n\tp = n\n\tpos = {}\n\tfor i in range(n):\n\t\tpos.setdefault(lst[i], i)\n\tfor i in range(n, 0, -1):\n\t\tif pos[i] <= p:\n\t\t\ta += lst[pos[i]:p]\n\t\t\tp = pos[i]\n\tprint(*a)\n", "class Solution:\n\n\tdef solution(self, nums):\n\t\tpoints = []\n\t\tmax_val = float('-inf')\n\t\tfor i in range(len(nums)):\n\t\t\tif nums[i] > max_val:\n\t\t\t\tmax_val = nums[i]\n\t\t\t\tpoints.append(i)\n\t\tresult = []\n\t\tlast = len(nums)\n\t\tfor point in points[::-1]:\n\t\t\tresult.extend(nums[point:last])\n\t\t\tlast = point\n\t\treturn result\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tnums = list(map(int, input().split()))\n\tsol = Solution()\n\tprint(str(sol.solution(nums))[1:-1].replace(', ', ' '))\n", "import sys, os, io\nfrom sys import stdin\nfrom math import log, gcd, ceil\nfrom collections import defaultdict, deque, Counter\nfrom heapq import heappush, heappop\nfrom bisect import bisect_left, bisect_right\nimport math\nalphabets = list('abcdefghijklmnopqrstuvwxyz')\n\ndef isPrime(x):\n\tfor i in range(2, x):\n\t\tif i * i > x:\n\t\t\tbreak\n\t\tif x % i == 0:\n\t\t\treturn False\n\treturn True\n\ndef ncr(n, r, p):\n\tnum = den = 1\n\tfor i in range(r):\n\t\tnum = num * (n - i) % p\n\t\tden = den * (i + 1) % p\n\treturn num * pow(den, p - 2, p) % p\n\ndef primeFactors(n):\n\tl = []\n\twhile n % 2 == 0:\n\t\tl.append(2)\n\t\tn = n / 2\n\tfor i in range(3, int(math.sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tl.append(int(i))\n\t\t\tn = n / i\n\tif n > 2:\n\t\tl.append(n)\n\treturn list(set(l))\n\ndef power(x, y, p):\n\tres = 1\n\tx = x % p\n\tif x == 0:\n\t\treturn 0\n\twhile y > 0:\n\t\tif y & 1 == 1:\n\t\t\tres = res * x % p\n\t\ty = y >> 1\n\t\tx = x * x % p\n\treturn res\n\ndef SieveOfEratosthenes(n):\n\tprime = [True for i in range(n + 1)]\n\tp = 2\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * p, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\treturn prime\n\ndef countdig(n):\n\tc = 0\n\twhile n > 0:\n\t\tn //= 10\n\t\tc += 1\n\treturn c\n\ndef si():\n\treturn input()\n\ndef prefix_sum(arr):\n\tr = [0] * (len(arr) + 1)\n\tfor (i, el) in enumerate(arr):\n\t\tr[i + 1] = r[i] + el\n\treturn r\n\ndef divideCeil(n, x):\n\tif n % x == 0:\n\t\treturn n // x\n\treturn n // x + 1\n\ndef ii():\n\treturn int(input())\n\ndef li():\n\treturn list(map(int, input().split()))\n\ndef ws(s):\n\tsys.stdout.write(s + '\\n')\n\ndef wi(n):\n\tsys.stdout.write(str(n) + '\\n')\n\ndef wia(a):\n\tsys.stdout.write(' '.join([str(x) for x in a]) + '\\n')\n\ndef power_set(L):\n\tcardinality = len(L)\n\tn = 2 ** cardinality\n\tpowerset = []\n\tfor i in range(n):\n\t\ta = bin(i)[2:]\n\t\tsubset = []\n\t\tfor j in range(len(a)):\n\t\t\tif a[-j - 1] == '1':\n\t\t\t\tsubset.append(L[j])\n\t\tpowerset.append(subset)\n\tpowerset_orderred = []\n\tfor k in range(cardinality + 1):\n\t\tfor w in powerset:\n\t\t\tif len(w) == k:\n\t\t\t\tpowerset_orderred.append(w)\n\treturn powerset_orderred\n\ndef fastPlrintNextLines(a):\n\tprint('\\n'.join(map(str, a)))\n\ndef sortByFirstAndSecond(A):\n\tA = sorted(A, key=lambda x: x[0])\n\tA = sorted(A, key=lambda x: x[1])\n\treturn list(A)\nif os.path.exists('input.txt'):\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('output.txt', 'w')\nelse:\n\tinput = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline\nt = 1\nt = int(input())\nfor _ in range(t):\n\tn = ii()\n\tl = li()\n\td = defaultdict(lambda : 0)\n\tfor i in range(n):\n\t\td[l[i]] = i\n\tmaxpre = [0] * n\n\tmaxpre[0] = l[0]\n\tfor i in range(1, n):\n\t\tmaxpre[i] = max(maxpre[i - 1], l[i])\n\tans = []\n\tend = n\n\ti = maxpre[-1]\n\twhile 1:\n\t\tans += l[i:end]\n\t\tif i == 0:\n\t\t\tbreak\n\t\tend = i\n\t\ti = d[maxpre[i - 1]]\n\tprint(*ans)\n", "def solve(arr, n):\n\tstack = []\n\tfinal_stack = []\n\tm = float('-inf')\n\tfor i in arr:\n\t\tif i > m:\n\t\t\tm = i\n\t\t\tfinal_stack.append(stack)\n\t\t\tstack = [i]\n\t\telse:\n\t\t\tstack.append(i)\n\tfinal_stack.append(stack)\n\tl = []\n\tfor i in final_stack[::-1]:\n\t\tl.extend(i)\n\treturn l\nfor _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tprint(*solve(arr, n), sep=' ')\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tmaxi = [l[0]]\n\tfor i in range(1, n):\n\t\tmaxi.append(max(l[i], maxi[i - 1]))\n\tans = []\n\ttemp = []\n\tl.append(l[-1])\n\tmaxi.append(maxi[-1])\n\tfor i in range(n):\n\t\ttemp.append(l[i])\n\t\tif maxi[i] != maxi[i + 1]:\n\t\t\tans.append(temp)\n\t\t\ttemp = []\n\tans.append(temp)\n\tfor i in range(len(ans) - 1, -1, -1):\n\t\tprint(*ans[i], end=' ')\n\tprint()\n", "import sys\nzz = 1\nsys.setrecursionlimit(10 ** 5)\nif zz:\n\tinput = sys.stdin.readline\nelse:\n\tsys.stdin = open('input.txt', 'r')\n\tsys.stdout = open('all.txt', 'w')\ndi = [[-1, 0], [1, 0], [0, 1], [0, -1]]\n\ndef fori(n):\n\treturn [fi() for i in range(n)]\n\ndef inc(d, c, x=1):\n\td[c] = d[c] + x if c in d else x\n\ndef ii():\n\treturn input().rstrip()\n\ndef li():\n\treturn [int(xx) for xx in input().split()]\n\ndef fli():\n\treturn [float(x) for x in input().split()]\n\ndef dadd(d, p, val):\n\tif p in d:\n\t\td[p].append(val)\n\telse:\n\t\td[p] = [val]\n\ndef gi():\n\treturn [xx for xx in input().split()]\n\ndef gtc(tc, ans):\n\tprint('Case #' + str(tc) + ':', ans)\n\ndef cil(n, m):\n\treturn n // m + int(n % m > 0)\n\ndef fi():\n\treturn int(input())\n\ndef pro(a):\n\treturn reduce(lambda a, b: a * b, a)\n\ndef swap(a, i, j):\n\t(a[i], a[j]) = (a[j], a[i])\n\ndef prec(a, pre):\n\tfor i in a:\n\t\tpre.append(pre[-1] + i)\n\tpre.pop(0)\n\ndef si():\n\treturn list(input().rstrip())\n\ndef mi():\n\treturn map(int, input().split())\n\ndef gh():\n\tsys.stdout.flush()\n\ndef isvalid(i, j, n, m):\n\treturn 0 <= i < n and 0 <= j < m\n\ndef bo(i):\n\treturn ord(i) - ord('a')\n\ndef graph(n, m):\n\tfor i in range(m):\n\t\t(x, y) = mi()\n\t\ta[x].append(y)\n\t\ta[y].append(x)\nt = fi()\nuu = t\nwhile t > 0:\n\tt -= 1\n\tn = fi()\n\ta = li()\n\tmix = n\n\tvis = {}\n\tans = []\n\tfor i in range(n - 1, -1, -1):\n\t\tif a[i] == mix:\n\t\t\tans.append(a[i])\n\t\t\tprint(*ans[::-1], end=' ')\n\t\t\tans = []\n\t\t\tvis[mix] = 1\n\t\t\twhile mix in vis:\n\t\t\t\tmix -= 1\n\t\telse:\n\t\t\tans.append(a[i])\n\t\t\tvis[a[i]] = 1\n\tprint(*ans[::-1])\n", "entrada = int(input())\nfor i in range(entrada):\n\tinpt = input()\n\tsize = [int(i) for i in input().split()]\n\tord = [0] * len(size)\n\tfor (i, card) in enumerate(size):\n\t\tord[card - 1] = i\n\tsizeN = []\n\twhile size:\n\t\ti_m_C = ord.pop()\n\t\tif i_m_C < len(size):\n\t\t\tsizeN += size[i_m_C:]\n\t\t\tdel size[i_m_C:]\n\tprint(' '.join(map(str, sizeN)))\n", "t = int(input())\nfor case in range(t):\n\tn = int(input())\n\tt = list(map(int, input().split()))\n\tmaxi_t = [0] * n\n\tmaxi = t[0]\n\tfor i in range(n):\n\t\tmaxi = max(maxi, t[i])\n\t\tmaxi_t[i] = maxi\n\trep = []\n\tsplit = n\n\tj = n - 1\n\twhile j >= 0:\n\t\twhile j >= 0 and t[j] != maxi_t[split - 1]:\n\t\t\tj -= 1\n\t\trep += t[j:split]\n\t\tsplit = j\n\t\tj -= 1\n\tprint(*rep)\n", "from math import *\nimport threading\nimport sys\nfrom collections import *\nmod = 10 ** 9\ninf = 10 ** 15\nyes = 'YES'\nno = 'NO'\n\ndef npr(n, r):\n\treturn factorial(n) // factorial(n - r) if n >= r else 0\n\ndef ncr(n, r):\n\treturn factorial(n) // (factorial(r) * factorial(n - r)) if n >= r else 0\n\ndef lower_bound(li, num):\n\tanswer = -1\n\tstart = 0\n\tend = len(li) - 1\n\twhile start <= end:\n\t\tmiddle = (end + start) // 2\n\t\tif li[middle] >= num:\n\t\t\tanswer = middle\n\t\t\tend = middle - 1\n\t\telse:\n\t\t\tstart = middle + 1\n\treturn answer\n\ndef upper_bound(li, num):\n\tanswer = -1\n\tstart = 0\n\tend = len(li) - 1\n\twhile start <= end:\n\t\tmiddle = (end + start) // 2\n\t\tif li[middle] <= num:\n\t\t\tanswer = middle\n\t\t\tstart = middle + 1\n\t\telse:\n\t\t\tend = middle - 1\n\treturn answer\n\ndef abs(x):\n\treturn x if x >= 0 else -x\n\ndef binary_search(li, val, lb, ub):\n\tans = -1\n\twhile lb <= ub:\n\t\tmid = (lb + ub) // 2\n\t\tif li[mid] > val:\n\t\t\tub = mid - 1\n\t\telif val > li[mid]:\n\t\t\tlb = mid + 1\n\t\telse:\n\t\t\tans = mid\n\t\t\tbreak\n\treturn ans\n\ndef kadane(x):\n\tsum_so_far = 0\n\tcurrent_sum = 0\n\tfor i in x:\n\t\tcurrent_sum += i\n\t\tif current_sum < 0:\n\t\t\tcurrent_sum = 0\n\t\telse:\n\t\t\tsum_so_far = max(sum_so_far, current_sum)\n\treturn sum_so_far\n\ndef pref(li):\n\tpref_sum = [0]\n\tfor i in li:\n\t\tpref_sum.append(pref_sum[-1] + i)\n\treturn pref_sum\n\ndef SieveOfEratosthenes(n):\n\tprime = [True for i in range(n + 1)]\n\tp = 2\n\tli = []\n\twhile p * p <= n:\n\t\tif prime[p] == True:\n\t\t\tfor i in range(p * p, n + 1, p):\n\t\t\t\tprime[i] = False\n\t\tp += 1\n\tfor p in range(2, len(prime)):\n\t\tif prime[p]:\n\t\t\tli.append(p)\n\treturn li\n\ndef primefactors(n):\n\tfactors = []\n\twhile n % 2 == 0:\n\t\tfactors.append(2)\n\t\tn //= 2\n\tfor i in range(3, int(sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tfactors.append(i)\n\t\t\tn //= i\n\tif n > 2:\n\t\tfactors.append(n)\n\treturn factors\n\ndef prod(li):\n\tans = 1\n\tfor i in li:\n\t\tans *= i\n\treturn ans\n\ndef dist(a, b):\n\td = abs(a[1] - b[1]) + abs(a[2] - b[2])\n\treturn d\n\ndef power_of_n(x, n):\n\tcnt = 0\n\twhile x % n == 0:\n\t\tcnt += 1\n\t\tx //= n\n\treturn cnt\n\ndef ask(l, r):\n\tif l == r:\n\t\treturn -1\n\tprint('?', l, r)\n\tsys.stdout.flush()\n\treturn int(input())\nimport itertools\nsys.setrecursionlimit(300000)\nfor _ in range(int(input()) if True else 1):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\td = defaultdict()\n\tfor i in range(n):\n\t\td[a[i]] = i\n\tind = []\n\tlast = n\n\tfor i in range(n, 0, -1):\n\t\tfor j in range(d[i], last):\n\t\t\tprint(a[j], end=' ')\n\t\t\ttemp = d[i]\n\t\tlast = temp\n\tprint('')\n", "t = int(input())\nfor i in range(t):\n\tn = int(input())\n\tk = list(map(int, input().split()))\n\tlista_card = n * [0]\n\tfor c in range(n):\n\t\tlista_card[k[c] - 1] = c\n\tsaida = []\n\tlista = k\n\tmenor = n\n\tfor e in range(n - 1, -1, -1):\n\t\tindice = lista_card[e]\n\t\tif indice <= menor:\n\t\t\tsaida += k[indice:menor]\n\t\t\tmenor = indice\n\tprint(' '.join(map(str, saida)))\n", "from sys import stdin, stdout\nnmbr = lambda : int(stdin.readline())\nlst = lambda : list(map(int, stdin.readline().split()))\nfor _ in range(nmbr()):\n\tn = nmbr()\n\ta = lst()\n\tp = 0\n\tp1 = n\n\tmx = max(a)\n\tdp = [0] * n\n\tfor i in range(n):\n\t\tdp[i] = max(dp[max(i - 1, 0)], a[i])\n\tfor i in range(n):\n\t\tif mx == a[i]:\n\t\t\tp = i\n\t\t\tbreak\n\twhile p >= 0:\n\t\tfor i in range(p, p1):\n\t\t\tstdout.write(str(a[i]) + ' ')\n\t\tp1 = p\n\t\tp -= 1\n\t\twhile p >= 0:\n\t\t\tif dp[p] == a[p]:\n\t\t\t\tbreak\n\t\t\tp -= 1\n\tprint()\n", "import math\nT = int(input())\nfor t in range(T):\n\tn = int(input())\n\tc = list(map(int, input().split()))\n\tm = c[0]\n\tk = [0]\n\tfor i in range(1, n):\n\t\tif c[i] > m:\n\t\t\tk.append(i)\n\t\t\tm = c[i]\n\tj = k[-1]\n\tk.pop()\n\ts = c[j:]\n\twhile len(k):\n\t\ts += c[k[-1]:j]\n\t\tj = k[-1]\n\t\tk.pop()\n\tprint(' '.join(map(str, s)))\n", "from heapq import heappop, heappush, heapify\nt = int(input())\nwhile t:\n\tt -= 1\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tans = []\n\ttemp = []\n\tpassed = set()\n\tcurr_max = n\n\tfor i in range(n - 1, -1, -1):\n\t\twhile curr_max > 0 and curr_max in passed:\n\t\t\tcurr_max -= 1\n\t\tif p[i] == curr_max:\n\t\t\ttemp.append(p[i])\n\t\t\tpassed.add(curr_max)\n\t\t\ttemp.reverse()\n\t\t\tfor j in temp:\n\t\t\t\tans.append(j)\n\t\t\ttemp = []\n\t\telse:\n\t\t\ttemp.append(p[i])\n\t\t\tpassed.add(p[i])\n\tfor i in ans:\n\t\tprint(i, end=' ')\n\tprint()\n", "t = int(input())\nwhile t:\n\tt = t - 1\n\tn = int(input())\n\tA = [int(x) for x in input().split()]\n\tZ = []\n\tB = dict()\n\tfor x in range(0, n + 1):\n\t\tB[x] = x\n\ti = n - 1\n\ts = 0\n\twhile i >= 0:\n\t\ts = s + 1\n\t\t(prev, nm) = B.popitem()\n\t\tif A[i] == nm:\n\t\t\tZ.extend(A[i:i + s])\n\t\t\ts = 0\n\t\telse:\n\t\t\tB[prev] = nm\n\t\ttry:\n\t\t\tdel B[A[i]]\n\t\texcept:\n\t\t\ta = 1\n\t\ti = i - 1\n\tprint(' '.join(list(map(str, Z))))\n", "import sys\n\ndef solve(cards):\n\tsegs = [[cards[0]]]\n\ttemp_max = cards[0]\n\tfor i in range(len(cards)):\n\t\tif i == 0:\n\t\t\tcontinue\n\t\tif cards[i] <= temp_max:\n\t\t\tsegs[-1].append(cards[i])\n\t\telse:\n\t\t\ttemp_max = cards[i]\n\t\t\tsegs.append([cards[i]])\n\treturn ' '.join([str(num) for seg in segs[::-1] for num in seg])\nt = int(sys.stdin.readline().strip())\nans = 0\nfor _ in range(t):\n\tn = sys.stdin.readline().strip()\n\tline = sys.stdin.readline().strip()\n\tcards = list(map(int, line.split()))\n\tprint(solve(cards))\n", "import os\nimport sys\nimport math\nfrom io import BytesIO, IOBase\ninput = sys.stdin.readline\n\ndef inp():\n\treturn int(input())\n\ndef inlt():\n\treturn list(map(int, input().split()))\n\ndef insr():\n\ts = input()\n\treturn list(s[:len(s) - 1])\n\ndef invr():\n\treturn map(int, input().split())\n\ndef dijkstra(start, distance, path, n):\n\tvisited = [False for _ in range(n)]\n\tdistance[start] = 0\n\tfor i in range(n):\n\t\tv = -1\n\t\tfor j in range(n):\n\t\t\tif not visited[v] and (v == -1 or distance[j] < distance[v]):\n\t\t\t\tv = j\n\t\tif distance[v] == math.inf:\n\t\t\tbreak\n\t\tvisited[v] = True\n\t\tfor edge in adj[v]:\n\t\t\tdestination = edge[0]\n\t\t\tweight = edge[1]\n\t\t\tif distance[v] + weight < distance[destination]:\n\t\t\t\tdistance[destination] = distance[v] + weight\n\t\t\t\tpath[destination] = v\n\ndef gcd(a, b):\n\tif b == 0:\n\t\treturn a\n\telse:\n\t\treturn gcd(b, a % b)\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef ncr(n, r):\n\treturn math.factorial(n) // (math.factorial(n - r) * math.factorial(r))\n\ndef npr(n, r):\n\treturn math.factorial(n) // math.factorial(n - r)\n\ndef seive(n):\n\tprimes = [True] * (n + 1)\n\tans = []\n\tfor i in range(2, n):\n\t\tif not primes[i]:\n\t\t\tcontinue\n\t\tj = 2 * i\n\t\twhile j <= n:\n\t\t\tprimes[j] = False\n\t\t\tj += i\n\tfor p in range(2, n + 1):\n\t\tif primes[p]:\n\t\t\tans += [p]\n\treturn ans\n\ndef factors(n):\n\tfactors = []\n\tx = 1\n\twhile x * x <= n:\n\t\tif n % x == 0:\n\t\t\tif n // x == x:\n\t\t\t\tfactors.append(x)\n\t\t\telse:\n\t\t\t\tfactors.append(x)\n\t\t\t\tfactors.append(n // x)\n\t\tx += 1\n\treturn factors\n\ndef main():\n\ttry:\n\t\tfor _ in range(inp()):\n\t\t\tn = inp()\n\t\t\ta = inlt()\n\t\t\td = {}\n\t\t\tans = []\n\t\t\tfor i in range(n):\n\t\t\t\td[a[i]] = i + 1\n\t\t\tmx = n\n\t\t\tstart = d[n]\n\t\t\twhile n > 0:\n\t\t\t\tans += a[start - 1:n]\n\t\t\t\tn = start - 1\n\t\t\t\tif n <= 0:\n\t\t\t\t\tbreak\n\t\t\t\twhile d[mx] > n:\n\t\t\t\t\tmx -= 1\n\t\t\t\tstart = d[mx]\n\t\t\tprint(*ans, sep=' ')\n\texcept Exception as e:\n\t\tprint(e)\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nmain()\n", "from collections import OrderedDict\nfor i in range(int(input())):\n\tn = int(input())\n\tinpList = list(map(int, input().split()))\n\tcheck_list = OrderedDict.fromkeys(range(1, n + 1))\n\tlast = check_list.popitem()[0]\n\tnew = []\n\tfor i in reversed(range(n)):\n\t\tif inpList[i] == last:\n\t\t\tfor j in inpList[i:n]:\n\t\t\t\tif j in check_list:\n\t\t\t\t\tcheck_list.pop(j)\n\t\t\t\tnew.append(j)\n\t\t\tn = i\n\t\t\tif len(check_list) > 0:\n\t\t\t\tlast = check_list.popitem()[0]\n\tnew += inpList[i:n]\n\tprint(' '.join([str(i) for i in new]))\n", "try:\n\tt = int(input())\n\twhile t != 0:\n\t\tn = int(input())\n\t\tarr = list(map(int, input().split()))\n\t\ttemp = []\n\t\tfor i in range(n):\n\t\t\ttemp.append([arr[i], i])\n\t\ttemp = sorted(temp, key=lambda x: -x[0])\n\t\tminimum = n\n\t\tp = []\n\t\tfor i in range(n):\n\t\t\tif i != 0 and minimum > temp[i][1]:\n\t\t\t\tp += arr[temp[i][1]:minimum]\n\t\t\t\tminimum = temp[i][1]\n\t\t\telif i == 0:\n\t\t\t\tp += arr[temp[i][1]:minimum]\n\t\t\t\tminimum = temp[i][1]\n\t\tprint(*p)\n\t\tt -= 1\nexcept EOFError:\n\tprint(' ')\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tres = []\n\tcurrent = a[0]\n\tstart = 0\n\tfor i in range(1, n):\n\t\tif a[i] > current:\n\t\t\tsth = a[start:i][::-1]\n\t\t\tstart = i\n\t\t\tcurrent = a[i]\n\t\t\tres += sth\n\tsth = a[start:][::-1]\n\tres += sth\n\tfor item in res[::-1]:\n\t\tprint(item, end=' ')\n\tprint('')\n", "from sys import stdin, stdout\nt = int(stdin.readline().strip())\noutputs = []\nfor __ in range(t):\n\tn = int(stdin.readline().strip())\n\ta = list(map(int, stdin.readline().strip().split()))\n\tmaxm_stack = [a[0]]\n\tfor i in range(1, n):\n\t\tmaxm_stack.append(max(maxm_stack[-1], a[i]))\n\t(cur, tot, new) = (1, 0, maxm_stack[-1])\n\treq = []\n\tfor i in range(n - 1, 0, -1):\n\t\tif new == maxm_stack[i - 1]:\n\t\t\treq.append(0)\n\t\t\ttot += 1\n\t\telse:\n\t\t\treq.append(cur)\n\t\t\tcur = cur + tot + 1\n\t\t\tnew = maxm_stack[i - 1]\n\t\t\ttot = 0\n\treq.append(cur)\n\treq.reverse()\n\tprev = None\n\tfor i in range(n):\n\t\tif req[i] == 0:\n\t\t\treq[i] = prev + 1\n\t\tprev = req[i]\n\tres = [''] * n\n\tfor i in range(n):\n\t\tres[req[i] - 1] = f'{a[i]}'\n\toutputs.append(' '.join(res))\nfor output in outputs:\n\tstdout.write(output + '\\n')\n", "import sys\ninput = sys.stdin.readline\nfor _ in range(int(input())):\n\t(n, cards, nums) = (int(input()), [int(i) for i in input().split()], {})\n\t(ans, last) = ([], n)\n\tfor i in range(n):\n\t\tnums[cards[i]] = i\n\tfor i in range(n, 0, -1):\n\t\tif nums[i] <= last:\n\t\t\tans += cards[nums[i]:last]\n\t\t\tlast = nums[i]\n\tprint(*ans)\n", "import sys\nimport math\nfrom bisect import bisect_left\nfrom collections import defaultdict\n\ndef get_lcp(s, suffix_array):\n\ts = s + '$'\n\tn = len(s)\n\tlcp = [0] * n\n\tpos = [0] * n\n\tfor i in range(n - 1):\n\t\tpos[suffix_array[i]] = i\n\tk = 0\n\tfor i in range(n - 1):\n\t\tif k > 0:\n\t\t\tk -= 1\n\t\tif pos[i] == n - 1:\n\t\t\tlcp[n - 1] = -1\n\t\t\tk = 0\n\t\t\tcontinue\n\t\telse:\n\t\t\tj = suffix_array[pos[i] + 1]\n\t\t\twhile max([i + k, j + k]) < n and s[i + k] == s[j + k]:\n\t\t\t\tk += 1\n\t\t\tlcp[pos[i]] = k\n\treturn lcp\n\ndef get_suffix_array(word):\n\tsuffix_array = [('', len(word))]\n\tfor position in range(len(word)):\n\t\tsliced = word[len(word) - position - 1:]\n\t\tsuffix_array.append((sliced, len(word) - position - 1))\n\tsuffix_array.sort(key=lambda x: x[0])\n\treturn [item[1] for item in suffix_array]\n\ndef get_ints():\n\treturn map(int, sys.stdin.readline().strip().split())\n\ndef bin_search(collection, element):\n\ti = bisect_left(collection, element)\n\tif i != len(collection) and collection[i] == element:\n\t\treturn i\n\telse:\n\t\treturn -1\n\ndef lcm(a, b):\n\tm = a * b\n\twhile a != 0 and b != 0:\n\t\tif a > b:\n\t\t\ta %= b\n\t\telse:\n\t\t\tb %= a\n\treturn m // (a + b)\n\ndef main():\n\tt = int(input())\n\tfor _ in range(t):\n\t\tn = int(input())\n\t\tp = list(map(int, input().split()))\n\t\tans = list()\n\t\tpref = []\n\t\tfor i in range(n):\n\t\t\tif i == 0:\n\t\t\t\tpref.append(p[i])\n\t\t\telse:\n\t\t\t\tpref.append(max(pref[i - 1], p[i]))\n\t\tlast = n - 1\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tif p[i] == pref[i]:\n\t\t\t\tans.append((i, last))\n\t\t\t\tlast = i - 1\n\t\tfor i in range(len(ans)):\n\t\t\tfor j in range(ans[i][0], ans[i][1] + 1):\n\t\t\t\tprint(p[j], end=' ')\n\t\tprint()\nmain()\n", "for _ in range(int(input())):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tp = [0] * (n + 1)\n\tfor (i, x) in enumerate(a):\n\t\tp[x] = i\n\tb = []\n\te = n\n\tfor x in range(n, 0, -1):\n\t\tif p[x] < e:\n\t\t\tb.extend(a[p[x]:e])\n\t\t\te = p[x]\n\tprint(*b)\n", "import sys\nimport math\nimport statistics\n\ndef function(n, p):\n\tmax1 = p[0]\n\tx = [0]\n\tans = []\n\tm = n\n\tfor i in range(n):\n\t\tif p[i] > max1:\n\t\t\tmax1 = p[i]\n\t\t\tx.append(i)\n\tx.reverse()\n\tfor i in x:\n\t\tfor j in range(i, m):\n\t\t\tans.append(p[j])\n\t\tm = i\n\tprint(*ans)\ninput = sys.stdin.read()\ndata = list(map(int, input.split()))\nt = data[0]\nl = 0\nfor i in range(t):\n\tn = data[1 + l]\n\tp = data[l + 2:l + 2 + n]\n\tl = l + 1 + n\n\tfunction(n, p)\n", "R1 = lambda : list(map(int, input().split()))\nR2 = lambda : int(input())\nt = R2()\nfor _ in range(t):\n\tn = R2()\n\tA = R1()\n\tres = ''\n\tmaxis = []\n\tm = 0\n\tfor i in range(n):\n\t\tif A[i] > m:\n\t\t\tm = A[i]\n\t\t\tmaxis.append(i)\n\tl = n\n\tfor i in maxis[::-1]:\n\t\tfor e in A[i:l]:\n\t\t\tres += str(e) + ' '\n\t\tl = i\n\tprint(res)\n", "from collections import deque\n\ndef main():\n\tt = int(input())\n\twhile t:\n\t\tn = int(input())\n\t\tL = [int(p) for p in input().split()]\n\t\ttemp = []\n\t\tbest = -1\n\t\tfor e in L:\n\t\t\tif e > best:\n\t\t\t\tbest = e\n\t\t\ttemp.append(best)\n\t\tres = deque()\n\t\tfor i in range(n - 1, -1, -1):\n\t\t\tres.appendleft(L[i])\n\t\t\tif L[i] == temp[i]:\n\t\t\t\tprint(*res, end=' ')\n\t\t\t\tres = deque()\n\t\tprint()\n\t\tt -= 1\nmain()\n", "def solution():\n\tn = int(input())\n\tp = [int(i) for i in input().split()]\n\tp_new = []\n\tpositions = [-1] * (n + 1)\n\tfor i in range(n):\n\t\tpositions[p[i]] = i\n\tlast_pos = n\n\tfor j in range(n, -1, -1):\n\t\tif positions[j] >= last_pos:\n\t\t\tcontinue\n\t\tp_new += p[positions[j]:last_pos]\n\t\tlast_pos = positions[j]\n\t\tif last_pos == 0:\n\t\t\tbreak\n\tprint(*p_new)\nfor t in range(int(input())):\n\tsolution()\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nimport math\nimport itertools\nimport bisect\nimport heapq\n\ndef main():\n\tpass\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef binary(n):\n\treturn bin(n).replace('0b', '')\n\ndef decimal(s):\n\treturn int(s, 2)\n\ndef pow2(n):\n\tp = 0\n\twhile n > 1:\n\t\tn //= 2\n\t\tp += 1\n\treturn p\n\ndef primeFactors(n):\n\tl = []\n\twhile n % 2 == 0:\n\t\tl.append(2)\n\t\tn = n / 2\n\tfor i in range(3, int(math.sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tl.append(i)\n\t\t\tn = n / i\n\tif n > 2:\n\t\tl.append(int(n))\n\treturn l\n\ndef isPrime(n):\n\tif n == 1:\n\t\treturn False\n\telse:\n\t\troot = int(n ** 0.5)\n\t\troot += 1\n\t\tfor i in range(2, root):\n\t\t\tif n % i == 0:\n\t\t\t\treturn False\n\t\treturn True\n\ndef maxPrimeFactors(n):\n\tmaxPrime = -1\n\twhile n % 2 == 0:\n\t\tmaxPrime = 2\n\t\tn >>= 1\n\tfor i in range(3, int(math.sqrt(n)) + 1, 2):\n\t\twhile n % i == 0:\n\t\t\tmaxPrime = i\n\t\t\tn = n / i\n\tif n > 2:\n\t\tmaxPrime = n\n\treturn int(maxPrime)\n\ndef countcon(s, i):\n\tc = 0\n\tch = s[i]\n\tfor i in range(i, len(s)):\n\t\tif s[i] == ch:\n\t\t\tc += 1\n\t\telse:\n\t\t\tbreak\n\treturn c\n\ndef lis(arr):\n\tn = len(arr)\n\tlis = [1] * n\n\tfor i in range(1, n):\n\t\tfor j in range(0, i):\n\t\t\tif arr[i] > arr[j] and lis[i] < lis[j] + 1:\n\t\t\t\tlis[i] = lis[j] + 1\n\tmaximum = 0\n\tfor i in range(n):\n\t\tmaximum = max(maximum, lis[i])\n\treturn maximum\n\ndef isSubSequence(str1, str2):\n\tm = len(str1)\n\tn = len(str2)\n\tj = 0\n\ti = 0\n\twhile j < m and i < n:\n\t\tif str1[j] == str2[i]:\n\t\t\tj = j + 1\n\t\ti = i + 1\n\treturn j == m\n\ndef maxfac(n):\n\troot = int(n ** 0.5)\n\tfor i in range(2, root + 1):\n\t\tif n % i == 0:\n\t\t\treturn n // i\n\treturn n\n\ndef p2(n):\n\tc = 0\n\twhile n % 2 == 0:\n\t\tn //= 2\n\t\tc += 1\n\treturn c\n\ndef seive(n):\n\tprimes = [True] * (n + 1)\n\tprimes[1] = primes[0] = False\n\tfor i in range(2, n + 1):\n\t\tif primes[i]:\n\t\t\tfor j in range(i + i, n + 1, i):\n\t\t\t\tprimes[j] = False\n\tp = []\n\tfor i in range(0, n + 1):\n\t\tif primes[i]:\n\t\t\tp.append(i)\n\treturn p\n\ndef ncr(n, r, p):\n\tnum = den = 1\n\tfor i in range(r):\n\t\tnum = num * (n - i) % p\n\t\tden = den * (i + 1) % p\n\treturn num * pow(den, p - 2, p) % p\n\ndef denofactinverse(n, m):\n\tfac = 1\n\tfor i in range(1, n + 1):\n\t\tfac = fac * i % m\n\treturn pow(fac, m - 2, m)\n\ndef numofact(n, m):\n\tfac = 1\n\tfor i in range(1, n + 1):\n\t\tfac = fac * i % m\n\treturn fac\n\ndef chk(n, d):\n\tif str(d) in str(n):\n\t\treturn True\n\treturn False\nfor xyz in range(0, int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tans = []\n\tind = [-1] * (n + 1)\n\tmaxtillnow = []\n\tfor i in range(0, n):\n\t\tind[l[i]] = i\n\t\tif i > 0:\n\t\t\tmaxtillnow.append(max(maxtillnow[-1], l[i]))\n\t\telse:\n\t\t\tmaxtillnow.append(l[i])\n\tprevind = n\n\titc = n - 1\n\twhile itc != -1:\n\t\ttind = maxtillnow[itc]\n\t\ttind2 = ind[tind]\n\t\tans += l[tind2:prevind]\n\t\tprevind = tind2\n\t\titc = prevind - 1\n\tprint(*ans)\n", "t = int(input())\nwhile t != 0:\n\tn = int(input())\n\tlist1 = list(map(int, input().split()))\n\tmaxi = [list1[0]]\n\tfor i in range(1, n):\n\t\tmaxi.append(max(maxi[-1], list1[i]))\n\tind = n - 1\n\tans = []\n\twhile ind >= 0:\n\t\tcurr = ind + 1\n\t\tnumber = maxi[ind]\n\t\twhile ind >= 0 and list1[ind] != number:\n\t\t\tind -= 1\n\t\tans.extend(list1[ind:curr])\n\t\tind -= 1\n\tprint(*ans)\n\tt -= 1\n", "import sys\ninput = sys.stdin.readline\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\toriginal_deck = list(map(int, input().split()))\n\tchkList = [False] * (n + 1)\n\tlast_num = original_deck[0]\n\tchkList[0] = True\n\tchkList[n] = True\n\tfor i in range(1, n):\n\t\tif last_num < original_deck[i]:\n\t\t\tchkList[i] = True\n\t\t\tlast_num = original_deck[i]\n\tfor i in range(n - 1, -1, -1):\n\t\tif chkList[i]:\n\t\t\tj = i + 1\n\t\t\tprint(original_deck[i], end=' ')\n\t\t\twhile not chkList[j]:\n\t\t\t\tprint(original_deck[j], end=' ')\n\t\t\t\tj += 1\n\tprint()\n", "import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time\nsys.setrecursionlimit(10 ** 7)\ninf = 10 ** 10\nmod = 10 ** 9 + 7\n\ndef LI():\n\treturn list(map(int, input().split()))\n\ndef II():\n\treturn int(input())\n\ndef LS():\n\treturn list(input().split())\n\ndef S():\n\treturn input()\n\ndef solve():\n\tn = II()\n\tcards = LI()\n\tcards_max = [0 for _ in range(n)]\n\tcards_max[0] = cards[0]\n\tfor i in range(1, n):\n\t\tcards_max[i] = max(cards_max[i - 1], cards[i])\n\tres = collections.deque()\n\tl = n - 1\n\tr = n\n\twhile r > 0:\n\t\tif cards_max[l] == cards[l]:\n\t\t\tres.extend(cards[l:r])\n\t\t\tl -= 1\n\t\t\tr = l + 1\n\t\telse:\n\t\t\tl -= 1\n\tres = [str(i) for i in res]\n\tprint(' '.join(list(res)))\n\ndef main():\n\tn = II()\n\tfor i in range(n):\n\t\tsolve()\n\treturn 0\nmain()\n", "def solve(p, n):\n\tposicoes = [0] * n\n\tfor j in range(n):\n\t\tposicoes[p[j] - 1] = j\n\tK = [posicoes[n - 1]]\n\tfor j in range(n - 2, -1, -1):\n\t\tif posicoes[j] < K[-1]:\n\t\t\tK.append(posicoes[j])\n\tresult = [0] * n\n\tright = n\n\tpos = 0\n\tfor left in K:\n\t\tfor j in range(right - left):\n\t\t\tresult[pos] = p[left + j]\n\t\t\tpos += 1\n\t\tright = left\n\treturn result\nt = int(input())\nfor i in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tprint(' '.join(map(str, solve(p, n))))\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().strip().split()))\n\tah = [i for i in range(n)]\n\td = dict(zip(l, ah))\n\tlan = []\n\td[0] = -1\n\tt = n\n\ta = n\n\twhile t > 0:\n\t\tlan += l[d[t]:a]\n\t\ta = d[t]\n\t\tfor i in range(t - 1, -1, -1):\n\t\t\tif d[i] < a:\n\t\t\t\tt = i\n\t\t\t\tbreak\n\tprint(*lan)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\n\ndef intArr():\n\treturn map(int, input().split())\n\ndef func(arr):\n\tn = len(arr)\n\tif n == 1:\n\t\treturn [1]\n\tl1 = [0] * n\n\tfor i in range(n):\n\t\tl1[arr[i] - 1] = i\n\tanswer = [0] * n\n\tidx = 0\n\tex = n\n\tcurr = n\n\twhile idx < n:\n\t\tp = l1[curr - 1]\n\t\twhile p < ex and idx < n:\n\t\t\tk = arr[p]\n\t\t\tanswer[idx] = k\n\t\t\tl1[k - 1] = -1\n\t\t\tp += 1\n\t\t\tidx += 1\n\t\tex = n - idx\n\t\twhile curr > 0 and l1[curr - 1] == -1:\n\t\t\tcurr -= 1\n\treturn answer\n\ndef main():\n\tfor _ in range(int(input())):\n\t\t_ = int(input())\n\t\tarr = list(intArr())\n\t\tprint(*func(arr))\n\treturn\nmain()\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tnums = [int(tmp) for tmp in input().split()]\n\tflags = [False] * (n + 1)\n\tcur = n - 1\n\tmax_cur = n\n\twhile cur >= 0:\n\t\twhile flags[max_cur]:\n\t\t\tmax_cur -= 1\n\t\tprev_cur = cur\n\t\twhile nums[cur] != max_cur:\n\t\t\tflags[nums[cur]] = True\n\t\t\tcur -= 1\n\t\tflags[nums[cur]] = True\n\t\tfor tmp in nums[cur:prev_cur + 1]:\n\t\t\tprint(tmp, end=' ')\n\t\tcur -= 1\n\tprint()\n", "for _ in range(int(input())):\n\tn = int(input())\n\tD = dict()\n\tres = []\n\tindex = 0\n\tA = [int(i) for i in input().split()]\n\tfor i in A:\n\t\tD[i] = index\n\t\tindex += 1\n\tx = n\n\tj = n\n\twhile D[x] != 0:\n\t\tif D[x] < j:\n\t\t\tres += A[D[x]:j]\n\t\t\tj = D[x]\n\t\tx -= 1\n\tres += A[D[x]:j]\n\tprint(*res)\n", "import os\nimport sys\nfrom io import BytesIO, IOBase\nimport math\nfrom decimal import *\ngetcontext().prec = 25\nMOD = pow(10, 9) + 7\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nfor _ in range(int(input())):\n\tn = int(input())\n\tl = list(map(int, input().split(' ')))\n\tf = [0] * n\n\tfor i in range(n):\n\t\tf[l[i] - 1] = i\n\tr = []\n\tz = n\n\tfor i in range(n - 1, -1, -1):\n\t\tif f[i] < z:\n\t\t\tr += l[f[i]:z]\n\t\t\tz = f[i]\n\tprint(*r)\n", "import sys, io, os, math\nfrom math import ceil, log, gcd, sqrt\nfrom itertools import permutations\nimport operator\nmod = 1000000007\nmod1 = 998244353\n\ndef intinp():\n\treturn int(sys.stdin.readline())\n\ndef strinp():\n\treturn sys.stdin.readline()\n\ndef arrinp():\n\treturn list(map(int, sys.stdin.readline().strip().split()))\n\ndef mulinp():\n\treturn map(int, sys.stdin.readline().strip().split())\n\ndef flush():\n\treturn stdout.flush()\n\ndef power_two(x):\n\treturn 1 << x\n\ndef lcm(a, b):\n\treturn a * b // gcd(a, b)\n\ndef onescomp(num, d):\n\treturn (1 << d) - 1 ^ num\n\ndef solve():\n\tn = intinp()\n\tt = arrinp()\n\tgrp = [[t[0]]]\n\tfor i in range(1, n):\n\t\tif grp[-1][0] > t[i]:\n\t\t\tgrp[-1].append(t[i])\n\t\telse:\n\t\t\tgrp.append([t[i]])\n\tgrp.sort(reverse=True)\n\tfor i in grp:\n\t\tprint(*i, end=' ')\n\tprint()\n\ndef main():\n\ttc = intinp()\n\twhile tc:\n\t\tsolve()\n\t\ttc -= 1\nmain()\n", "import sys\nimport collections as cc\nimport math as mt\ninput = sys.stdin.readline\nI = lambda : list(map(int, input().split()))\nfor tc in range(int(input())):\n\t(n,) = I()\n\tar = I()\n\tans = []\n\tfl = n\n\tte = n\n\tvisi = [0] * (n + 1)\n\tvisi[n] = 1\n\tfor i in range(n - 1, -1, -1):\n\t\tvisi[ar[i]] = 1\n\t\tif ar[i] == fl:\n\t\t\tans += ar[i:te]\n\t\t\tte = i\n\t\t\twhile visi[fl]:\n\t\t\t\tfl -= 1\n\tans += ar[:te]\n\tprint(*ans)\n", "t = int(input())\n\ndef ceil(a, b):\n\tif a % b:\n\t\treturn a // b + 1\n\treturn a // b\nfor _ in range(t):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\tmaxi = [0]\n\tfor i in range(n):\n\t\tmaxi.append(max(maxi[-1], l[i]))\n\tdel maxi[0]\n\tj = n - 1\n\tans = []\n\twhile j >= 0:\n\t\tm = 1\n\t\tj -= 1\n\t\twhile j >= 0 and maxi[j] == maxi[j + 1]:\n\t\t\tj -= 1\n\t\t\tm += 1\n\t\te = j + 1\n\t\twhile m:\n\t\t\tans.append(l[e])\n\t\t\te += 1\n\t\t\tm -= 1\n\tprint(*ans)\n", "casos = int(input())\nfor a in range(casos):\n\tuso = []\n\ttamanho = int(input())\n\tfinal = [0] * tamanho\n\tsequencia = list(map(int, input().split()))\n\tmenor = len(sequencia)\n\tmaior = len(sequencia)\n\tfor b in range(tamanho):\n\t\tfinal[sequencia[b] - 1] = b\n\tfor c in range(-1, -1 * len(final) - 1, -1):\n\t\tif final[c] < menor:\n\t\t\tmenor = final[c]\n\t\t\tfor d in range(menor, maior, 1):\n\t\t\t\tuso.append(sequencia[d])\n\t\t\tmaior = menor\n\tprint(*uso)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tl2 = []\n\ta = p[0]\n\ts = [0]\n\tfor i in range(1, n):\n\t\tif p[i] > a:\n\t\t\ta = p[i]\n\t\t\ts.append(i)\n\ts = s[::-1]\n\tind = n\n\tfor j in s:\n\t\tl2.extend(p[j:ind])\n\t\tind = j\n\tprint(*l2)\n", "import operator\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\ta = list(map(int, input().split()))\n\tmaxi = 0\n\tnew_a = []\n\tlocal_max_points = []\n\tfor i in range(len(a)):\n\t\tif a[i] > maxi:\n\t\t\tmaxi = a[i]\n\t\t\tlocal_max_points.append(i)\n\tlast_point = len(a)\n\tfor point in local_max_points[::-1]:\n\t\tnew_a += a[point:last_point]\n\t\tlast_point = point\n\tprint(*new_a)\n", "import sys\nfrom itertools import accumulate\nimport os\nfrom io import BytesIO, IOBase\nfrom math import ceil\nBUFSIZE = 8192\n\nclass FastIO(IOBase):\n\tnewlines = 0\n\n\tdef __init__(self, file):\n\t\tself._fd = file.fileno()\n\t\tself.buffer = BytesIO()\n\t\tself.writable = 'x' in file.mode or 'r' not in file.mode\n\t\tself.write = self.buffer.write if self.writable else None\n\n\tdef read(self):\n\t\twhile True:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tif not b:\n\t\t\t\tbreak\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines = 0\n\t\treturn self.buffer.read()\n\n\tdef readline(self):\n\t\twhile self.newlines == 0:\n\t\t\tb = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))\n\t\t\tself.newlines = b.count(b'\\n') + (not b)\n\t\t\tptr = self.buffer.tell()\n\t\t\t(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))\n\t\tself.newlines -= 1\n\t\treturn self.buffer.readline()\n\n\tdef flush(self):\n\t\tif self.writable:\n\t\t\tos.write(self._fd, self.buffer.getvalue())\n\t\t\t(self.buffer.truncate(0), self.buffer.seek(0))\n\nclass IOWrapper(IOBase):\n\n\tdef __init__(self, file):\n\t\tself.buffer = FastIO(file)\n\t\tself.flush = self.buffer.flush\n\t\tself.writable = self.buffer.writable\n\t\tself.write = lambda s: self.buffer.write(s.encode('ascii'))\n\t\tself.read = lambda : self.buffer.read().decode('ascii')\n\t\tself.readline = lambda : self.buffer.readline().decode('ascii')\n(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))\ninput = lambda : sys.stdin.readline().rstrip('\\r\\n')\nT = int(input())\nfor _ in range(T):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tpos = {v: i for (i, v) in enumerate(arr)}\n\tans = []\n\tright = n\n\tfor i in range(n, 0, -1):\n\t\tif pos[i] > right:\n\t\t\tcontinue\n\t\tans.extend(arr[pos[i]:right])\n\t\tright = pos[i]\n\tprint(*ans)\n", "import math\nfrom collections import defaultdict\nfrom sys import stdin\ninput = stdin.readline\nT = int(input())\nfor _ in range(T):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tl = []\n\tfor i in range(n):\n\t\tl.append([arr[i], i])\n\tl.sort(reverse=True)\n\tans = []\n\ts = set()\n\tfor i in l:\n\t\tind = i[1]\n\t\twhile ind < n:\n\t\t\tif ind in s:\n\t\t\t\tbreak\n\t\t\telse:\n\t\t\t\tans.append(arr[ind])\n\t\t\t\ts.add(ind)\n\t\t\t\tind += 1\n\tprint(*ans)\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tcurMaxs = [0 for _ in range(n)]\n\tfor i in range(1, n):\n\t\tcurMaxs[i] = curMaxs[i - 1]\n\t\tif p[i] > p[curMaxs[i]]:\n\t\t\tcurMaxs[i] = i\n\tx = n - 1\n\tres = []\n\twhile x >= 0:\n\t\tres += p[curMaxs[x]:x + 1]\n\t\tx = curMaxs[x] - 1\n\tprint(' '.join([str(x) for x in res]))\n", "n = int(input())\nfor i in range(n):\n\tresp = ''\n\ttam = int(input())\n\tposicoes = [0] * (tam + 1)\n\ty = list(map(int, input().strip().split()))[:tam]\n\tx = [0]\n\tx.extend(y)\n\tfor i in range(1, tam + 1):\n\t\tposicoes[x[i]] = i\n\ta = tam\n\tb = tam + 1\n\twhile a > 0:\n\t\tif posicoes[a] >= b:\n\t\t\ta -= 1\n\t\telse:\n\t\t\tfor j in range(posicoes[a], b):\n\t\t\t\tresp += str(x[j])\n\t\t\t\tresp += ' '\n\t\t\tb = posicoes[a]\n\tprint(resp.strip())\n", "import re\nimport sys\nexit = sys.exit\nfrom bisect import bisect_left as bsl, bisect_right as bsr\nfrom collections import Counter, defaultdict as ddict, deque\nfrom functools import lru_cache\ncache = lru_cache(None)\nfrom heapq import *\nfrom itertools import *\nfrom math import inf\nfrom pprint import pprint as pp\nenum = enumerate\nri = lambda : int(rln())\nris = lambda : list(map(int, rfs()))\nrln = sys.stdin.readline\nrl = lambda : rln().rstrip('\\n')\nrfs = lambda : rln().split()\nd4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]\nd8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]\nt = ri()\nfor _ in range(t):\n\tn = ri()\n\tp = ris()\n\thp = []\n\tfor i in range(n):\n\t\theappush(hp, (-p[i], i))\n\tans = []\n\tk = n\n\twhile hp:\n\t\t(_, i) = heappop(hp)\n\t\tif i >= k:\n\t\t\tcontinue\n\t\tfor j in range(i, k):\n\t\t\tans.append(p[j])\n\t\tk = i\n\tprint(*ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tlis = list(map(int, input().split()))\n\tindex = []\n\tmaxa = 0\n\tfor i in range(len(lis)):\n\t\tif lis[i] > maxa:\n\t\t\tindex.append(i)\n\t\t\tmaxa = lis[i]\n\tindex = index[::-1]\n\tl = lis[index[0]:]\n\tfor i in range(1, len(index)):\n\t\tl.extend(lis[index[i]:index[i - 1]])\n\tprint(*l)\n", "for _ in range(int(input())):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tseen = set(list(range(1, n + 1)))\n\tsearch = n\n\ti = n - 1\n\tans = []\n\twhile i >= 0:\n\t\twhile search not in seen:\n\t\t\tsearch -= 1\n\t\tstart = i\n\t\ttemp = []\n\t\twhile arr[i] != search:\n\t\t\ttemp.append(arr[i])\n\t\t\tseen.remove(arr[i])\n\t\t\ti -= 1\n\t\ttemp.append(arr[i])\n\t\tseen.remove(arr[i])\n\t\tans += temp[::-1]\n\t\ti -= 1\n\tprint(*ans)\n", "def int_fn():\n\treturn int(input())\n\ndef str_fn():\n\treturn input()\n\ndef int_list_fn():\n\treturn [int(val) for val in input().split(' ')]\n\ndef solve(n, cards):\n\thash_arr = [0] * (n + 1)\n\tfor (idx, card) in enumerate(cards):\n\t\thash_arr[card] = idx\n\ti = n\n\toutput = []\n\tlimit = n\n\twhile i > 0:\n\t\tstart_from = hash_arr[i]\n\t\tif start_from > limit:\n\t\t\ti -= 1\n\t\t\tcontinue\n\t\tfor j in range(start_from, limit):\n\t\t\toutput.append(str(cards[j]))\n\t\tlimit = start_from\n\t\ti -= 1\n\tprint(' '.join(output))\n\treturn\nfor _ in range(int_fn()):\n\tn = int_fn()\n\tcards = int_list_fn()\n\tsolve(n, cards)\npass\n", "for _ in range(int(input())):\n\tn = int(input())\n\tp = list(map(int, input().split()))\n\tl1 = p.copy()\n\tl1.sort()\n\tm = n - 1\n\td1 = dict()\n\tfor i in range(n):\n\t\td1[l1[i]] = i\n\tans = []\n\tstore = n\n\tfor i in range(n - 1, -1, -1):\n\t\tif p[i] == l1[m]:\n\t\t\tans += p[i:store]\n\t\t\tstore = i\n\t\t\tl1[d1[p[i]]] = -1\n\t\t\twhile l1[m] == -1 and m > -1:\n\t\t\t\tm -= 1\n\t\telse:\n\t\t\tl1[d1[p[i]]] = -1\n\tprint(*ans)\n", "import sys\nimport math\n\ndef read_ints():\n\tinp = input().split()\n\tinp = [int(x) for x in inp]\n\treturn inp\n\ndef read_strings():\n\tinp = input()\n\ts = [inp[i] for i in range(len(inp))]\n\treturn s\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tp = read_ints()\n\telements = [1 for i in range(len(p))]\n\tind = n - 1\n\ti = n - 1\n\tans = []\n\twhile i >= 0:\n\t\ttmp = []\n\t\twhile p[i] != ind + 1 and i >= 0:\n\t\t\telements[p[i] - 1] = 0\n\t\t\ttmp.append(p[i])\n\t\t\ti -= 1\n\t\tif i < 0:\n\t\t\tbreak\n\t\ttmp.append(p[i])\n\t\tfor j in range(len(tmp) - 1, -1, -1):\n\t\t\tans.append(tmp[j])\n\t\telements[p[i] - 1] = 0\n\t\ti -= 1\n\t\tind -= 1\n\t\twhile elements[ind] == 0 and ind >= 0:\n\t\t\tind -= 1\n\tans = [str(x) for x in ans]\n\tprint(' '.join(ans))\n", "T = int(input())\nfor t in range(T):\n\tN = int(input())\n\tarr = [int(k) for k in input().split()]\n\tmapping = {}\n\tfor (i, num) in enumerate(arr):\n\t\tmapping[num] = i\n\tres = []\n\tright = N\n\tfor i in range(N, 0, -1):\n\t\tidx = mapping[i]\n\t\tif idx >= right:\n\t\t\tcontinue\n\t\tres += arr[idx:right]\n\t\tright = idx\n\tfor x in res:\n\t\t(print(x, end=' '),)\n\tprint()\n", "class SparseTable:\n\n\tdef __init__(self, A, ide_ele, f):\n\t\tself.n = len(A)\n\t\tself.f = f\n\t\tmax_k = self.n.bit_length() - 1\n\t\tself.ide_ele = ide_ele\n\t\tself.table = [[ide_ele] * (max_k + 1) for i in range(self.n)]\n\t\tfor i in range(self.n):\n\t\t\tself.table[i][0] = A[i]\n\t\tfor k in range(1, max_k + 1):\n\t\t\tk2 = 1 << k - 1\n\t\t\tk3 = (1 << k) - 1\n\t\t\tfor i in range(self.n - k3):\n\t\t\t\tself.table[i][k] = self.f(self.table[i][k - 1], self.table[i + k2][k - 1])\n\n\tdef query(self, l, r):\n\t\tif l >= r:\n\t\t\treturn self.ide_ele\n\t\td = r - l\n\t\tif d == 1:\n\t\t\treturn self.table[l][0]\n\t\tk = (d - 1).bit_length() - 1\n\t\tk2 = 1 << k\n\t\treturn self.f(self.table[l][k], self.table[r - k2][k])\nINF = 10 ** 18\nimport sys\nimport io, os\ninput = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tP = list(map(int, input().split()))\n\tst = SparseTable(P, 0, max)\n\tans = []\n\tcur = n - 1\n\twhile cur >= 0:\n\t\tM = st.query(0, cur + 1)\n\t\ttemp = []\n\t\tfor i in range(cur, -1, -1):\n\t\t\ttemp.append(P[i])\n\t\t\tif P[i] == M:\n\t\t\t\tcur = i - 1\n\t\t\t\tbreak\n\t\ttemp.reverse()\n\t\tfor p in temp:\n\t\t\tans.append(p)\n\tprint(*ans)\n"] | {"inputs": ["4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1\n", "4\n4\n2 1 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1\n", "4\n4\n2 1 3 4\n5\n1 5 2 4 3\n6\n2 4 5 3 6 1\n1\n1\n", "4\n4\n1 2 3 4\n5\n1 5 2 4 3\n6\n4 2 5 3 6 1\n1\n1\n"], "outputs": ["4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1\n", "4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1\n", "4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1\n", "\n4 3 2 1\n5 2 4 3 1\n6 1 5 3 4 2\n1\n"]} | EASY | ['data structures', 'greedy', 'math'] | null | codeforces | ['Data structures', 'Mathematics', 'Greedy algorithms'] | ['Data structures', 'Greedy algorithms'] | https://codeforces.com/problemset/problem/1492/B | null | 1 second | 2021-02-23 | 0 | 512 megabytes | null |
|
"The number obtained by multiplying 1 by 2, 3, 5 several times (0 or more times) is called the Hammi(...TRUNCATED) | "[\"import sys\\nfrom sys import stdin\\nfrom bisect import bisect_right, bisect_left\\nfrom math im(...TRUNCATED) | "{\"inputs\": [\"3 8\\n2 27\\n1 86\\n0\", \"3 9\\n2 27\\n1 86\\n0\", \"3 8\\n1 35\\n1 86\\n0\", \"5 (...TRUNCATED) | UNKNOWN_DIFFICULTY | [] | null | aizu | [] | [] | null | null | 1.0 seconds | null | null | 134.217728 megabytes | null |
|
"Given a number s(in string form). Find the Smallest number (Not leading Zeros) which can be obtaine(...TRUNCATED) | "[\"class Solution:\\n\\n\\tdef minimum_Number(self, s):\\n\\t\\tl = list(s)\\n\\t\\tl.sort()\\n\\t\(...TRUNCATED) | "#User function Template for python3\n\n\n\nclass Solution:\n\n\tdef minimum_Number(self, s):\n\n\t\(...TRUNCATED) | "{\"inputs\": [\"s = \\\"846903\\\"\", \"s = \\\"55010\\\"\"], \"outputs\": [\"304689\", \"10055\"]}(...TRUNCATED) | EASY | ['Algorithms', 'Mathematical'] | null | geeksforgeeks | ['Mathematics'] | [] | "https://practice.geeksforgeeks.org/problems/smallest-number-by-rearranging-digits-of-a-given-number(...TRUNCATED) | O(1) | null | null | 0 | null | O(N * log(N)) where N is the number of digits of the given number |
"Dilpreet wants to paint his dog's home that has n boards with different lengths. The length of i^{t(...TRUNCATED) | "[\"class Solution:\\n\\n\\tdef minTime(self, arr, n, k):\\n\\n\\t\\tdef numofPainter(maxLen):\\n\\t(...TRUNCATED) | "#User function Template for python3\n\nclass Solution:\n def minTime (self, arr, n, k):\n (...TRUNCATED) | "{\"inputs\": [\"n = 5\\nk = 3\\narr[] = {5,10,30,20,15}\", \"n = 4\\nk = 2\\narr[] = {10,20,30,40}\(...TRUNCATED) | MEDIUM_HARD | ['Algorithms', 'Searching', 'Binary Search', 'Divide and Conquer', 'Dynamic Programming'] | null | geeksforgeeks | ['Dynamic programming', 'Sorting', 'Divide and conquer', 'Complete search'] | ['Dynamic programming', 'Sorting', 'Complete search'] | https://practice.geeksforgeeks.org/problems/the-painters-partition-problem1535/1 | O(1) | null | null | 0 | null | O(n log m) , m = sum of all boards' length |
"An **anagram** is the result of rearranging the letters of a word to produce a new word.\n\n**Note:(...TRUNCATED) | "[\"def is_anagram(test, original):\\n\\treturn sorted(original.lower()) == sorted(test.lower())\\n\(...TRUNCATED) | def is_anagram(test, original):
| "{\"fn_name\": \"is_anagram\", \"inputs\": [[\"foefet\", \"toffee\"], [\"Buckethead\", \"DeathCubeK\(...TRUNCATED) | EASY | ['Strings', 'Fundamentals'] | null | codewars | ['String algorithms', 'Fundamentals'] | [] | https://www.codewars.com/kata/529eef7a9194e0cbc1000255 | null | null | null | null | null | null |
"There are n cities in Berland. Some pairs of them are connected with m directed roads. One can use (...TRUNCATED) | [] | "{\"inputs\": [\"4 0 1\\n3 1 1\\n\", \"20 20 40\\n4 5\\n6 7\\n13 14\\n10 11\\n9 10\\n19 20\\n2 3\\n1(...TRUNCATED) | VERY_HARD | ['trees', 'dfs and similar', 'graphs'] | null | codeforces | ['Tree algorithms', 'Graph algorithms', 'Graph traversal'] | [] | https://codeforces.com/problemset/problem/864/F | null | 2.0 seconds | null | null | 256.0 megabytes | null |
End of preview. Expand
in Dataset Viewer.
Copied from BAAI/TACO.
Changes:
- train + test sets merged (by concatenate_dataset)
- Custom python code removed. (To enable dataset viewer in the HF web UI)
- Downloads last month
- 81