id
int64
1
25.4k
question
stringlengths
29
14k
solutions
sequencelengths
1
6.38k
starter_code
stringlengths
0
955
input_output
stringlengths
33
74M
difficulty
stringclasses
6 values
raw_tags
stringlengths
2
277
name
nulllengths
4
105
βŒ€
source
stringclasses
9 values
tags
stringlengths
2
183
skill_types
stringclasses
120 values
url
stringlengths
36
108
βŒ€
Expected Auxiliary Space
nullclasses
4 values
time_limit
stringclasses
130 values
date
timestamp[us]
picture_num
stringclasses
8 values
memory_limit
stringclasses
28 values
Expected Time Complexity
nullclasses
9 values
1
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-31T00:00:00
null
null
null
3
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
4
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", "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", "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", "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", "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", "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", "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", "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", "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 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", "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", "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", "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", "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 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 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", "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", "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", "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", "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", "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", "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 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", "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", "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", "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", "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-23T00:00:00
0
512 megabytes
null
8
An **anagram** is the result of rearranging the letters of a word to produce a new word. **Note:** anagrams are case insensitive Complete the function to return `true` if the two arguments given are anagrams of each other; return `false` otherwise. ## Examples * `"foefet"` is an anagram of `"toffee"` * `"Buckethead"` is an anagram of `"DeathCubeK"`
[ "def is_anagram(test, original):\n\treturn sorted(original.lower()) == sorted(test.lower())\n", "from collections import Counter\n\ndef is_anagram(test, original):\n\treturn Counter(test.lower()) == Counter(original.lower())\n", "def is_anagram(test, original):\n\treturn sorted(test.upper()) == sorted(original.upper())\n", "def is_anagram(test, original):\n\t(test_dict, original_dict) = ({}, {})\n\tfor i in test.lower():\n\t\ttest_dict[i] = test_dict.get(i, 0) + 1\n\tfor i in original.lower():\n\t\toriginal_dict[i] = original_dict.get(i, 0) + 1\n\treturn test_dict == original_dict\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\tcount = [0] * 26\n\tfor i in range(len(test)):\n\t\tcount[(ord(test[i]) & 31) - 1] += 1\n\t\tcount[(ord(original[i]) & 31) - 1] -= 1\n\treturn not any(count)\n", "def is_anagram(test, original):\n\ta = sorted(test.lower())\n\tb = sorted(original.lower())\n\tc = ''.join(a)\n\td = ''.join(b)\n\tif c == d:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tgo = len(test) == len(original)\n\tarr = []\n\tif go:\n\t\tfor i in test:\n\t\t\tarr.append(i.lower() in original.lower())\n\t\treturn False not in arr\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\tfor l in test.lower():\n\t\tif l not in original.lower():\n\t\t\treturn False\n\treturn True\n", "from operator import eq\nfrom collections import Counter\n\ndef is_anagram(test, original):\n\treturn eq(*map(Counter, map(str.lower, (test, original))))\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\treturn sorted(test.lower()) == sorted(original.lower())\n", "def is_anagram(test, original):\n\tif sorted(test.lower()) == sorted(original.lower()):\n\t\treturn True\n\telse:\n\t\treturn False\n", "is_anagram = lambda t, o: sorted(t.lower()) == sorted(o.lower())\n", "aprime = {'a': 2, 'c': 5, 'b': 3, 'e': 11, 'd': 7, 'g': 17, 'f': 13, 'i': 23, 'h': 19, 'k': 31, 'j': 29, 'm': 41, 'l': 37, 'o': 47, 'n': 43, 'q': 59, 'p': 53, 's': 67, 'r': 61, 'u': 73, 't': 71, 'w': 83, 'v': 79, 'y': 97, 'x': 89, 'z': 101}\n\ndef aprime_sum(str):\n\tstrChList = list(str.lower())\n\treturn sum([aprime[x] for x in strChList])\n\ndef is_anagram(test, original):\n\tif aprime_sum(test) == aprime_sum(original):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\treturn set(original.lower()) == set(test.lower()) if len(test) == len(original) else False\n", "def is_anagram(test, original):\n\ta = list(test.lower())\n\ts = list(original.lower())\n\tif len(a) != len(s):\n\t\treturn False\n\telse:\n\t\tfor i in a:\n\t\t\tcond = False\n\t\t\tk = 0\n\t\t\twhile k != len(s) and cond == False:\n\t\t\t\tif i == s[k]:\n\t\t\t\t\ta.remove(i)\n\t\t\t\t\ts.remove(i)\n\t\t\t\t\tcond = True\n\t\t\t\tk += 1\n\t\t\tif cond == False:\n\t\t\t\treturn False\n\t\tif len(a) != len(s):\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n", "def is_anagram(test, original):\n\tflag = 0\n\tif len(test) != len(original):\n\t\treturn False\n\telse:\n\t\tfor i in test.lower():\n\t\t\tif i not in original.lower():\n\t\t\t\tflag = 1\n\t\t\telse:\n\t\t\t\tcontinue\n\t\tif flag == 1:\n\t\t\treturn False\n\t\telse:\n\t\t\treturn True\n", "def is_anagram(test, original):\n\n\tdef to_dict(word):\n\t\tdictionary = {}\n\t\tfor w in word.lower():\n\t\t\tif w not in dictionary:\n\t\t\t\tdictionary[w] = 0\n\t\t\telse:\n\t\t\t\tdictionary[w] += 1\n\t\treturn dictionary\n\treturn to_dict(test) == to_dict(original)\n", "is_anagram = lambda a, b, s=sorted: s(a.lower()) == s(b.lower())\n", "def is_anagram(s, l):\n\tn = len(s)\n\tif len(l) != n:\n\t\treturn False\n\ts = s.lower()\n\tl = l.lower()\n\th = [0 for x in range(26)]\n\tfor i in range(n):\n\t\th[ord(s[i]) - 97] += 1\n\t\th[ord(l[i]) - 97] -= 1\n\treturn h.count(0) == 26\n", "def is_anagram(test: str, original: str) -> bool:\n\treturn all([all([_ in original.lower() for _ in test.lower()]), len(test) == len(original)])\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\ttestcount = 0\n\tfor i in test:\n\t\tif i in original:\n\t\t\ttestcount += 1\n\toriginalcount = 0\n\tfor i in original:\n\t\tif i in test:\n\t\t\toriginalcount += 1\n\tif testcount == originalcount and testcount == len(test) and (originalcount == len(original)):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) == len(original):\n\t\ttest = test.lower()\n\t\toriginal = original.lower()\n\t\tcount = 0\n\t\tfor char in test:\n\t\t\tif char in original:\n\t\t\t\tcount += 1\n\t\tif count == len(test):\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest_list = []\n\toriginal_list = []\n\tfor i in test.lower():\n\t\ttest_list.append(i)\n\tfor i in original.lower():\n\t\toriginal_list.append(i)\n\ttest_list.sort()\n\toriginal_list.sort()\n\tprint(test_list)\n\tprint(original_list)\n\tif test_list == original_list:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\tletters = {}\n\tfor i in test.lower():\n\t\tif i in letters:\n\t\t\tletters[i] += 1\n\t\telse:\n\t\t\tletters[i] = 1\n\tfor i in original.lower():\n\t\tif i not in letters:\n\t\t\treturn False\n\t\tif original.lower().count(i) != letters[i]:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(t, o):\n\treturn sorted([*t.lower()]) == sorted([*o.lower()])\n", "def is_anagram(test, original):\n\tx = list(test.lower())\n\ty = list(original.lower())\n\tx = sorted(x)\n\ty = sorted(y)\n\tif x == y:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\ta = sorted(test.lower())\n\tb = sorted(original.lower())\n\tif a == b:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tsorted_test = sorted(list(test.lower()))\n\tsorted_original = sorted(list(original.lower()))\n\treturn sorted_test == sorted_original\n", "def is_anagram(test, original):\n\tletters = [c for c in test.lower()]\n\tfor char in original.lower():\n\t\tif char in letters:\n\t\t\tdel letters[letters.index(char)]\n\t\telse:\n\t\t\treturn False\n\treturn not bool(len(letters))\n", "import collections\n\ndef is_anagram(test, original):\n\treturn collections.Counter([i.lower() for i in sorted(test)]) == collections.Counter([i.lower() for i in sorted(original)])\n", "def is_anagram(test, original):\n\ttest_set = sorted(test.lower())\n\toriginal_set = sorted(original.lower())\n\tif test_set == original_set:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tt = sorted(test.lower())\n\to = sorted(original.lower())\n\tif t == o:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tnew_test = test.lower()\n\tnew_original = original.lower()\n\tsortedTest = sorted(new_test)\n\tsortedOriginal = sorted(new_original)\n\tfor letters in new_test:\n\t\tif letters in new_original and len(new_test) == len(new_original) and (sortedOriginal == sortedTest):\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n", "def is_anagram(test, original):\n\ttest_word_freq = {}\n\toriginal_word_freq = {}\n\ttest = test.lower()\n\toriginal = original.lower()\n\tif len(test) == len(original):\n\t\tfor (idx, letter) in enumerate(test):\n\t\t\tif letter not in test_word_freq:\n\t\t\t\ttest_word_freq[letter] = 1\n\t\t\telse:\n\t\t\t\ttest_word_freq[letter] += 1\n\t\tfor (idx, lett) in enumerate(original):\n\t\t\tif lett not in original_word_freq:\n\t\t\t\toriginal_word_freq[lett] = 1\n\t\t\telse:\n\t\t\t\toriginal_word_freq[lett] += 1\n\t\tprint(original_word_freq)\n\t\tprint(test_word_freq)\n\t\tfor (k, v) in list(test_word_freq.items()):\n\t\t\tif k not in original_word_freq:\n\t\t\t\treturn False\n\t\t\tif v != original_word_freq[k]:\n\t\t\t\treturn False\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tfirst = [i.lower() for i in test]\n\tsecond = [i.lower() for i in original]\n\treturn sorted(first) == sorted(second)\n", "def is_anagram(test, original):\n\tlist_test = []\n\tlist_original = []\n\tfor i in test.lower():\n\t\tlist_test += i\n\tfor i in original.lower():\n\t\tlist_original += i\n\tif len(list_test) == len(list_original):\n\t\tlist_test.sort()\n\t\tlist_original.sort()\n\t\tif list_test == list_original:\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\treturn True if sorted([letter for letter in test.lower()]) == sorted([letter for letter in original.lower()]) else False\n", "def is_anagram(test, original):\n\tt = list(test.lower())\n\tto = ''.join(sorted(t))\n\to = list(original.lower())\n\too = ''.join(sorted(o))\n\tif to == oo:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tletterCount = dict.fromkeys('abcdefghijklmnopqrstuvwxyz', 0)\n\tfor c in test.lower():\n\t\tletterCount[c] += 1\n\tfor c in original.lower():\n\t\tletterCount[c] -= 1\n\tfor value in list(letterCount.values()):\n\t\tif value != 0:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(a_str, b_str):\n\tif len(a_str) == len(b_str):\n\t\ta_list = list(a_str.lower())\n\t\tb_list = list(b_str.lower())\n\t\tfor char in a_list:\n\t\t\tif char in b_list:\n\t\t\t\tb_list.remove(char)\n\t\tif not b_list:\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\telse:\n\t\ttest = test.lower()\n\t\toriginal = original.lower()\n\t\tcounter_original = [0] * 26\n\t\tcounter_test = [0] * 26\n\t\tfor i in test:\n\t\t\tcounter_test[ord(i) - 97] += 1\n\t\tfor i in original:\n\t\t\tcounter_original[ord(i) - 97] += 1\n\treturn counter_test == counter_original\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tnewList = [ord(c) for c in test]\n\tnewList.sort()\n\tnewList2 = [ord(b) for b in original]\n\tnewList2.sort()\n\tif newList == newList2:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tcounterTest = [0] * 255\n\tcounterOri = [0] * 255\n\tfor i in range(len(test)):\n\t\tcounterTest[ord(test[i].lower())] += 1\n\tfor i in range(len(original)):\n\t\tcounterOri[ord(original[i].lower())] += 1\n\tif counterOri == counterTest:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = test.upper()\n\toriginal = original.upper()\n\tif sorted(test) == sorted(original):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tif len(test) == len(original):\n\t\ttest = test.lower()\n\t\toriginal = original.lower()\n\t\tfor i in test:\n\t\t\tif original.find(i) == -1:\n\t\t\t\treturn False\n\t\t\telse:\n\t\t\t\ttest.replace(i, '')\n\t\t\t\toriginal.replace(i, '')\n\telse:\n\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tcounter1 = [0] * 255\n\tcounter2 = [0] * 255\n\tfor i in range(len(test)):\n\t\tcounter1[ord(test[i].lower())] += 1\n\tfor i in range(len(original)):\n\t\tcounter2[ord(original[i].lower())] += 1\n\treturn counter1 == counter2\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tfor x in range(len(test)):\n\t\tif test.count(test[x]) != original.count(test[x]):\n\t\t\treturn False\n\tfor x in range(len(original)):\n\t\tif test.count(original[x]) != original.count(original[x]):\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tnT = len(test)\n\tnO = len(original)\n\tif nO == nT:\n\t\tcounterT = [0] * (255 + 1)\n\t\tcounterO = [0] * (255 + 1)\n\t\tfor x in range(nT):\n\t\t\tcounterT[ord(test[x])] += 1\n\t\t\tcounterO[ord(original[x])] += 1\n\t\tif counterT == counterO:\n\t\t\treturn True\n\t\telse:\n\t\t\treturn False\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tn = len(original)\n\tif n != len(test):\n\t\treturn False\n\tcounterTest = [0] * 255\n\tcounterOrig = [0] * 255\n\tfor i in range(n):\n\t\tcounterTest[ord(test[i].lower())] += 1\n\t\tcounterOrig[ord(original[i].lower())] += 1\n\treturn True if ''.join(map(str, counterTest)) == ''.join(map(str, counterOrig)) else False\n", "def is_anagram(test, original):\n\treturn sorted([n.lower() for n in test]) == sorted([n.lower() for n in original])\n", "def is_anagram(word_o, test_o):\n\tis_anagram = True\n\tword = word_o.lower()\n\ttest = test_o.lower()\n\tif len(word) != len(test):\n\t\tis_anagram = False\n\talist = list(test.lower())\n\tpos1 = 0\n\twhile pos1 < len(word) and is_anagram:\n\t\tpos2 = 0\n\t\tfound = False\n\t\twhile pos2 < len(alist) and (not found):\n\t\t\tif word[pos1] == alist[pos2]:\n\t\t\t\tfound = True\n\t\t\telse:\n\t\t\t\tpos2 = pos2 + 1\n\t\tif found:\n\t\t\talist[pos2] = None\n\t\telse:\n\t\t\tis_anagram = False\n\t\tpos1 = pos1 + 1\n\treturn is_anagram\n", "def is_anagram(test, original):\n\tl1 = list(test.lower())\n\tl2 = list(original.lower())\n\tif len(l1) == len(l2):\n\t\tfor i in l1:\n\t\t\tif i in l2:\n\t\t\t\tl2.remove(i)\n\t\t\telse:\n\t\t\t\treturn False\n\telse:\n\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tfor i in test.lower():\n\t\tif i in original.lower() and len(test) == len(original):\n\t\t\tcontinue\n\t\telse:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest_list = [letter1 for letter1 in test.lower()]\n\torig_list = [letter2 for letter2 in original.lower()]\n\tif sorted(test_list) == sorted(orig_list):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tt = sorted(test.lower())\n\to = sorted(original.lower())\n\tif t == o:\n\t\tprint('true')\n\t\treturn True\n\telse:\n\t\tprint('false')\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = [i.lower() for i in test]\n\toriginal = [j.lower() for j in original]\n\ttest.sort()\n\toriginal.sort()\n\treturn test == original\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tif len(test) != len(original):\n\t\treturn False\n\tfor x in test:\n\t\tif test.count(x) == original.count(x):\n\t\t\tcontinue\n\t\telse:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tnew_test = list(test)\n\tnew_original = list(original)\n\tnew_test.sort()\n\tnew_original.sort()\n\tif new_test == new_original:\n\t\treturn True\n\treturn False\n\tpass\n", "def is_anagram(test, original):\n\treturn set(test.upper()) == set(original.upper()) and len(test) == len(original)\n", "is_anagram = lambda test, original: True if sorted(original.lower()) == sorted(test.lower()) else False\n", "def is_anagram(test, original):\n\toriginalLower = [val for val in original.lower()]\n\tarr = test.lower()\n\tif len(arr) != len(originalLower):\n\t\treturn False\n\tfor element in arr:\n\t\tif element not in originalLower:\n\t\t\treturn False\n\t\telse:\n\t\t\toriginalLower.remove(element)\n\treturn True\n", "def is_anagram(test, original):\n\tn1 = len(test)\n\tn2 = len(original)\n\tif n1 != n2:\n\t\treturn False\n\tstr1 = sorted(test.lower())\n\tstr2 = sorted(original.lower())\n\tfor i in range(0, n1):\n\t\tif str1[i] != str2[i]:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\ttest_l = list(test.lower())\n\toriginal_l = list(original.lower())\n\ttest_l.sort()\n\toriginal_l.sort()\n\tif test_l == original_l:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = list(test.lower())\n\toriginal = list(original.lower())\n\tif len(test) != len(original):\n\t\treturn False\n\tfor word in test:\n\t\tfor word2 in original:\n\t\t\tif word == word2:\n\t\t\t\toriginal.remove(word2)\n\t\t\t\tbreak\n\tif len(original) == 0:\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ta = sorted(list(test.lower()))\n\tb = sorted(list(original.lower()))\n\tif a == b:\n\t\tprint(f'The word {test} is an anagram of {original}')\n\t\treturn True\n\telse:\n\t\tprint(f'Characters do not match for test case {test}, {original}')\n\t\treturn False\n", "def is_anagram(test, original):\n\n\tdef to_list(string):\n\t\tlisted = []\n\t\tfor i in range(len(string)):\n\t\t\tlisted.append(string[i])\n\t\treturn listed\n\treturn str(sorted(to_list(test.lower()))) == str(sorted(to_list(original.lower())))\n", "def is_anagram(test, original):\n\ttest = list(test.lower())\n\ttest.sort()\n\toriginal = list(original.lower())\n\toriginal.sort()\n\tif original != test or len(test) != len(original):\n\t\treturn False\n\telse:\n\t\treturn True\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\ttest = sorted(test.lower())\n\toriginal = sorted(original.lower())\n\tfor i in range(len(test)):\n\t\tif test[i] != original[i]:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tresult = True if len(test) == len(original) else False\n\tfor letter in test.upper():\n\t\tresult = False if letter not in original.upper() else result\n\treturn result\n", "def is_anagram(test, original):\n\tif len(original) != len(test):\n\t\treturn False\n\ttest = test.lower()\n\toriginal = original.lower()\n\tfor letter in original:\n\t\tif original.count(letter) != test.count(letter):\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tif sorted(test.lower()) == sorted(original.lower()):\n\t\treturn True\n\telif test != original:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest_list = sorted(list(test.lower()))\n\toriginal_list = sorted(list(original.lower()))\n\tif test_list == original_list:\n\t\treturn True\n\tif test_list != original_list:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttest = test.lower()\n\toriginal = original.lower()\n\tt = list(test)\n\to = list(original)\n\tt.sort()\n\to.sort()\n\treturn t == o\n", "def is_anagram(test, original):\n\tt = test.lower()\n\to = [*original.lower()]\n\tif len(t) != len(o):\n\t\treturn False\n\tfor c in t:\n\t\tif c in o:\n\t\t\to.remove(c)\n\t\telse:\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tif len(test) > len(original) or len(test) < len(original):\n\t\treturn False\n\tres = ''\n\tcounter = 0\n\tsortedTest = sorted(test.lower())\n\tsortedOriginal = sorted(original.lower())\n\tfor i in range(0, len(sortedTest)):\n\t\tif sortedTest[i] != sortedOriginal[i]:\n\t\t\tres = False\n\t\t\tbreak\n\t\telse:\n\t\t\tres = True\n\treturn res\n", "from collections import Counter as C\n\ndef is_anagram(test, original):\n\treturn C(test.lower()) == C(original.lower())\n", "def is_anagram(test, original):\n\tsort1 = sorted(test.lower())\n\tsort2 = sorted(original.lower())\n\tif ''.join(sort2) == ''.join(sort1):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\ttheTest = test.lower()\n\ttheOriginal = original.lower()\n\tif len(theTest) != len(theOriginal):\n\t\treturn False\n\telse:\n\t\tindex = 0\n\t\tlengthCheck = 0\n\t\tarray = [None] * len(theTest)\n\t\tfor i in theOriginal:\n\t\t\tarray[index] = i\n\t\t\tindex += 1\n\t\tfor j in theTest:\n\t\t\ttestLength = len(theTest)\n\t\t\tif j in array:\n\t\t\t\tlengthCheck += 1\n\t\t\telse:\n\t\t\t\treturn False\n\t\tif lengthCheck == testLength:\n\t\t\treturn True\n", "def is_anagram(tst, org):\n\ttst = tst.lower()\n\torg = org.lower()\n\tif len(tst) != len(org):\n\t\treturn False\n\tfor i in org:\n\t\tif tst.count(i) != org.count(i):\n\t\t\treturn False\n\treturn True\n", "def is_anagram(test, original):\n\tif len(test) != len(original):\n\t\treturn False\n\telif sorted(test.casefold()) == sorted(original.casefold()):\n\t\treturn True\n\telse:\n\t\treturn False\n", "def is_anagram(test, original):\n\tletters_original = sorted(list(original.upper()))\n\tletters_test = sorted(list(test.upper()))\n\treturn letters_original == letters_test\n", "def is_anagram(test, original):\n\treturn len(test) == len(original) and all([i in original.lower() for i in test.lower()])\n", "def is_anagram(test, original):\n\torg1 = [x.lower() for x in original]\n\torg2 = [y.lower() for y in test]\n\torg1.sort()\n\torg2.sort()\n\tif org1 == org2:\n\t\treturn True\n\treturn False\n", "def is_anagram(test, original):\n\toriginal_list = list(original.lower())\n\ttest_list = list(test.lower())\n\toriginal_list.sort()\n\ttest_list.sort()\n\ta = ''.join(test_list)\n\tb = ''.join(original_list)\n\treturn a == b\n", "def is_anagram(test, original):\n\ttest = test.lower().replace(' ', '')\n\toriginal = original.lower().replace(' ', '')\n\tif len(test) != len(original):\n\t\treturn False\n\tfor letter in test:\n\t\tif letter not in original:\n\t\t\treturn False\n\tfor letter in original:\n\t\tif letter not in test:\n\t\t\treturn False\n\treturn True\n" ]
def is_anagram(test, original):
{"fn_name": "is_anagram", "inputs": [["foefet", "toffee"], ["Buckethead", "DeathCubeK"], ["Twoo", "WooT"], ["dumble", "bumble"], ["ound", "round"], ["apple", "pale"]], "outputs": [[true], [true], [true], [false], [false], [false]]}
EASY
['Strings', 'Fundamentals']
null
codewars
['String algorithms', 'Fundamentals']
[]
https://www.codewars.com/kata/529eef7a9194e0cbc1000255
null
null
null
null
null
null
10
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to mi. Define di as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of di over all days. There are no marks on the channel before the first day. Input The first line contains a single positive integer n (1 ≀ n ≀ 105) β€” the number of days. The second line contains n space-separated integers m1, m2, ..., mn (0 ≀ mi < i) β€” the number of marks strictly above the water on each day. Output Output one single integer β€” the minimum possible sum of the number of marks strictly below the water level among all days. Examples Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 Note In the first example, the following figure shows an optimal case. <image> Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. <image>
[ "n = int(input())\nabove = list(map(int, input().split()))\ntotal = [x + 1 for x in above]\nfor i in range(0, n - 1)[::-1]:\n\ttotal[i] = max(total[i], total[i + 1] - 1)\nfor i in range(1, n):\n\ttotal[i] = max(total[i], total[i - 1])\nbelow = [t - a - 1 for (t, a) in zip(total, above)]\nprint(sum(below))\n", "from sys import stdin, stdout\n\ndef rint():\n\treturn map(int, stdin.readline().split())\nn = int(input())\nu = list(rint())\nu = [0] + u\nmark = 0\nb = [0]\nfor i in range(1, n + 1):\n\tuu = u[i]\n\tb.append(i)\n\tif uu >= mark:\n\t\tinc = uu - mark + 1\n\t\tl = len(b)\n\t\tfor i in range(inc):\n\t\t\tb.pop()\n\t\tmark += inc\ntot = [1 for i in range(n + 1)]\nfor bb in b:\n\ttot[bb] = 0\nfor i in range(1, n + 1):\n\ttot[i] = tot[i - 1] + tot[i]\nans = 0\nfor i in range(1, n + 1):\n\tans += tot[i] - u[i] - 1\nprint(ans)\n", "n = int(input())\nm = list(map(int, input().split()))\na = [0] * n\nk = 0\nfor i in range(n):\n\tk = max(k, m[i] + 1)\n\ta[i] = k\nfor i in range(n - 1, 0, -1):\n\ta[i - 1] = max(a[i] - 1, a[i - 1])\nans = 0\nfor i in range(n):\n\tans += a[i] - m[i] - 1\nprint(ans)\n" ]
{"inputs": ["3\n0 1 1\n", "4\n0 0 1 2\n", "2\n0 0\n", "4\n0 1 1 0\n", "3\n0 1 0\n", "2\n0 1\n", "8\n0 0 2 0 3 0 3 2\n", "3\n0 1 2\n", "10\n0 0 2 2 3 2 3 3 1 3\n", "6\n0 0 0 2 0 1\n", "10\n0 1 2 0 4 5 3 6 0 5\n", "4\n0 0 1 1\n", "3\n0 0 0\n", "9\n0 1 0 1 1 4 0 4 8\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "3\n0 0 1\n", "5\n0 1 0 3 1\n", "4\n0 1 0 3\n", "7\n0 1 1 3 0 0 6\n", "1\n0\n", "3\n0 0 2\n", "4\n0 1 1 1\n", "10\n0 0 1 2 3 2 3 3 1 3\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "5\n0 1 0 3 0\n", "7\n0 1 2 3 0 0 6\n", "5\n0 1 2 0 2\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "7\n0 0 1 3 0 0 6\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "10\n0 0 2 2 1 2 3 3 1 3\n", "10\n0 1 2 0 4 0 3 6 0 5\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "10\n0 0 1 2 3 2 3 0 1 3\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 16 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 0 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 21 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 69 45 43 4\n", "10\n0 1 2 0 4 0 3 6 1 5\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 71 37 37 38 39 28 0 2 23 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 16 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 0 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 0 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 30 31 31 31 0 3 15 31 13 33 6 35 35 35 36 36 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 8 28 0 2 26 41 9 9 0 6 25 41 41 12 42 20 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 21 14 8 15 15 15 19 25 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 3 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 69 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 71 37 37 38 39 28 0 2 23 41 9 5 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 9 12 16 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 8 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 39 28 0 0 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 10 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 8 28 0 2 26 41 9 9 0 6 25 41 41 12 42 20 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 21 14 8 15 15 15 19 25 7 17 17 18 19 9 10 5 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 30 0 5 8 28 29 48 31 31 31 0 3 3 31 8 33 6 35 35 35 36 0 37 37 38 39 28 0 2 26 41 9 9 0 0 25 41 41 12 42 43 43 36 44 69 45 43 4\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 14 5 3 28 29 30 31 31 31 0 3 15 31 8 33 6 35 35 35 36 71 37 37 38 39 28 0 2 23 41 9 5 0 6 25 41 41 12 42 43 43 36 44 51 11 43 4\n", "4\n0 1 0 2\n", "4\n0 1 0 0\n", "6\n0 0 0 1 0 1\n", "4\n0 0 1 0\n", "4\n0 1 1 3\n", "5\n0 1 1 1 2\n", "5\n0 1 2 1 4\n", "6\n0 1 1 3 0 2\n", "4\n0 1 2 1\n", "5\n0 1 0 1 0\n", "100\n0 1 2 2 3 0 1 5 6 6 0 0 8 7 1 9 9 4 10 11 12 2 12 12 12 12 9 13 14 8 15 15 15 19 15 7 17 17 18 19 9 10 21 0 22 9 2 24 24 4 24 7 25 0 5 8 28 29 48 31 31 31 0 3 15 31 8 33 6 35 35 35 36 36 37 37 38 8 28 0 2 26 41 9 9 0 6 25 41 41 12 42 43 43 36 44 51 45 43 4\n", "6\n0 0 0 1 1 1\n", "5\n0 1 2 0 4\n", "6\n0 0 1 3 0 2\n", "4\n0 0 2 1\n", "5\n0 1 1 1 0\n", "10\n0 1 1 0 4 0 3 6 1 5\n", "6\n0 0 1 3 0 3\n", "4\n0 0 2 0\n", "5\n0 1 1 2 0\n", "10\n0 1 1 1 4 0 3 6 1 5\n", "6\n0 0 0 3 0 3\n", "5\n0 1 1 2 2\n", "5\n0 1 2 1 2\n", "6\n0 1 0 3 0 2\n"], "outputs": ["0\n", "0\n", "0\n", "1\n", "1\n", "0\n", "7\n", "0\n", "4\n", "4\n", "12\n", "0\n", "0\n", "17\n", "761\n", "0\n", "4\n", "2\n", "10\n", "0\n", "1\n", "0\n", "3\n", "758\n", "5\n", "9\n", "2\n", "772\n", "11\n", "1479\n", "1515\n", "1510\n", "4\n", "16\n", "795\n", "6\n", "774\n", "773\n", "1549\n", "1771\n", "15\n", "2658\n", "776\n", "768\n", "1533\n", "1543\n", "1783\n", "2662\n", "779\n", "1528\n", "1559\n", "1789\n", "2667\n", "1\n", "2\n", "1\n", "1\n", "1\n", "0\n", "2\n", "5\n", "1\n", "2\n", "1510\n", "0\n", "3\n", "6\n", "2\n", "1\n", "16\n", "5\n", "3\n", "2\n", "15\n", "6\n", "0\n", "1\n", "6\n"]}
MEDIUM_HARD
['data structures', 'greedy', 'dp']
null
codeforces
['Dynamic programming', 'Data structures', 'Greedy algorithms']
['Dynamic programming', 'Data structures', 'Greedy algorithms']
https://codeforces.com/problemset/problem/957/D
null
1.0 seconds
null
null
256.0 megabytes
null
12
Tom has finally taken over the business empire and now looking for a new Name of the business to make a new start. Joe (Tom's dear friend) suggested a string $S$ consisting of Uppercase and lowercase letters Tom wants to make some changes as per the following criteria: 1) String should $not$ have any vowels . 2) Every other uppercase consonant(other characters except vowels) should be in lowercase For ex: If the consonant character is Z then it should be z 3) There should be a character "." before each consonant. Help Tom to make the required Changes. -----Input:----- - First line will contain string $S$,This string only consists of uppercase and lowercase letters. -----Output:----- Print the resulting string. It is guaranteed that this string is not empty. -----Constraints----- - Length of string is in [1 .. 100] -----Sample Input:----- $CodeSprInT$ -----Sample Output:----- .c.d.s.p.r.n.t -----EXPLANATION:----- C is a consonant and it is in uppercase so turn it in lower case and add a β€œ.” before it o is a vowel so it is deleted d is a consonant and in lowercase so just add a β€œ.” before it e is a vowel so it is deleted S is a consonant and it is in uppercase so turn it in lower case and add a β€œ.” before it p is a consonant and in lowercase so just add a β€œ.” before it r is a consonant and in lowercase so just add a β€œ.” before it I is a vowel so it is deleted n is a consonant and in lowercase so just add a β€œ.” before it T is a consonant and it is in uppercase so turn it in lower case and add a β€œ.” before it
[ "s = input().lower()\nvow = ['a', 'e', 'i', 'o', 'u', 'y']\nans = ''\nfor ch in s:\n\tif ch in vow:\n\t\tcontinue\n\tif ch.isalpha():\n\t\tans += '.' + ch\nprint(ans)\n" ]
{"inputs": [["CodeSprInT"]], "outputs": [[".c.d.s.p.r.n.t"]]}
UNKNOWN_DIFFICULTY
[]
null
codechef
[]
[]
https://www.codechef.com/SPRT2020/problems/EMPRNM
null
null
null
null
null
null
13
When Chef was born, his parents took him to the famous monk Doctor Strange to know whether he will land himself in heaven after his life or not. According to Strange, Chef will live for $L$ years in total. If he wants to go to heaven, he must spend at least $50\%$ of his life years doing good deeds. He also shows them his future using a string $S$ of length $L$ where $S_{i} = 0$ means the $i$-th year will be counted as bad as per the rule books of heaven and $S_{i} = 1$ means the $i$-th year will be counted as good. Also, Strange can use his special powers to make Chef end his life earlier than that planned by god, i.e, he can choose some $L'$ ($1≀ L'≀ L$) and make him live for only $L' $ years. Strange wants Chef to succeed, so if there is any choice of $L'$ that allows Chef to go to heaven, he will do so. Tell whether Chef can go to heaven. ------ Input ------ The first line contains an integer $T$, the number of test cases. Then the test cases follow. Each test case contains two lines of input. The first line contains a single integer $L$. The second line contains a string $S$ of length $L$, consisting of symbols 0 and 1. ------ Output ------ For each test case, output the answer in a single line: "YES" if Chef can go to heaven and "NO" if not (without quotes). You may print each character of each string in uppercase or lowercase (for example, the strings "yEs", "yes", "Yes" and "YES" will all be treated as identical). ------ Constraints ------ $1 ≀ L ≀ 10^{5}$ The sum of $L$ over all tests does not exceed $10^{6}$ ------ Subtasks ------ Subtask #1 (100 points): original constraints ----- Sample Input 1 ------ 3 2 10 3 001 4 0100 ----- Sample Output 1 ------ YES NO YES ----- explanation 1 ------ Test case 1: If Chef lives for the complete $2$ years, he will have a total of $1$ good year which is $\frac{1 * 100}{2} = 50\%$ of his life, and hence he will go to heaven. Test case 2: There's no way Chef can go to heaven. Test case 3: If Chef lives for $2$ years, he will have a total of $1$ good year which is $\frac{1 * 100}{2} = 50\%$ of his life, and hence he will go to heaven.
[ "for i in range(0, int(input())):\n\tn = int(input())\n\tl = list(input())\n\tt = 0\n\ta = 0\n\tb = 0\n\tfor j in range(0, n):\n\t\tt = t + 1\n\t\tif l[j] == '0':\n\t\t\ta = a + 1\n\t\telse:\n\t\t\tb = b + 1\n\t\tif b >= t / 2:\n\t\t\tprint('YES')\n\t\t\tbreak\n\t\telif j == n - 1 and b < t / 2:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tcontinue\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\t(c, c1) = (0, 0)\n\tans = 'NO'\n\tfor i in s:\n\t\tif i == '1':\n\t\t\tc1 += 1\n\t\tc += 1\n\t\tif c1 * 2 >= c:\n\t\t\tans = 'YES'\n\t\t\tbreak\n\tprint(ans)\n", "t = int(input())\nfor ni in range(t):\n\tl = int(input())\n\ts = input()\n\t(s0, s1) = (0, 0)\n\tpos = 'NO'\n\tfor i in range(len(s)):\n\t\tif s[i] == '0':\n\t\t\ts0 = s0 + 1\n\t\telse:\n\t\t\ts1 = s1 + 1\n\t\tif s1 >= s0:\n\t\t\tpos = 'YES'\n\t\t\tbreak\n\tprint(pos)\n", "t = int(input())\nwhile t:\n\tl = int(input())\n\ts = input()\n\tif s.count('1') >= s.count('0'):\n\t\tprint('YES')\n\telse:\n\t\ta = 0\n\t\tflag = False\n\t\tfor i in range(0, l):\n\t\t\tif s[i] == '1':\n\t\t\t\ta += 1\n\t\t\t\tif a >= (i + 1) / 2:\n\t\t\t\t\tflag = True\n\t\t\t\t\tbreak\n\t\tif flag == True:\n\t\t\tprint('YES')\n\t\telse:\n\t\t\tprint('NO')\n\tt -= 1\n", "a = int(input(''))\nfor i in range(a):\n\tx = int(input(''))\n\tt = input('')\n\tc1 = 0\n\tc0 = 0\n\tflag = 0\n\tfor j in range(len(t)):\n\t\tif t[j] == '1':\n\t\t\tc1 = c1 + 1\n\t\telse:\n\t\t\tc0 = c0 + 1\n\t\tif c1 >= c0:\n\t\t\tflag = 1\n\tprint('YES' if flag else 'NO')\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\tcount1 = 0\n\tcount0 = 0\n\tflag = 0\n\tfor i in range(len(s)):\n\t\tif s[i] == '1':\n\t\t\tcount1 += 1\n\t\telse:\n\t\t\tcount0 += 1\n\t\tif count1 >= count0:\n\t\t\tflag = 1\n\tprint('YES' if flag else 'NO')\n", "t = int(input())\nfor i in range(t):\n\tL = int(input())\n\tS = input()\n\tb = 0\n\tg = 0\n\tflag = 0\n\tfor j in range(L):\n\t\tif S[j] == '0':\n\t\t\tb = b + 1\n\t\telif S[j] == '1':\n\t\t\tg = g + 1\n\t\tif g >= b:\n\t\t\tflag = 1\n\t\t\tbreak\n\tif flag == 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n", "t = int(input())\nfor t in range(t):\n\tl = int(input())\n\ts = input()\n\tcount_0 = 0\n\tcount_1 = 0\n\tflag = 0\n\tfor i in range(len(s)):\n\t\tif s[i] == '0':\n\t\t\tcount_0 += 1\n\t\telse:\n\t\t\tcount_1 += 1\n\t\tif count_1 >= count_0 and count_0 != 0 or (count_0 == 0 and count_1 > 0):\n\t\t\tflag = 1\n\t\t\tbreak\n\tif flag == 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n", "for _ in range(int(input())):\n\tL = int(input())\n\tS = input().strip()\n\tif S.count('1') >= L // 2 + (L % 2 > 0):\n\t\tprint('YES')\n\t\tcontinue\n\tcount = 0\n\tyrs = 0\n\tfor c in S:\n\t\tcount += c == '1'\n\t\tyrs += 1\n\t\tif count >= yrs // 2 + (yrs % 2 > 0):\n\t\t\tprint('YES')\n\t\t\tbreak\n\telse:\n\t\tprint('NO')\n", "for tc in range(int(input())):\n\tL = int(input())\n\tS = input()\n\t(g, t) = (0, 0)\n\tans = 'NO'\n\tfor i in S:\n\t\tt += 1\n\t\tif i == '1':\n\t\t\tg += 1\n\t\tif g / t >= 0.5:\n\t\t\tans = 'YES'\n\t\t\tbreak\n\t\telse:\n\t\t\tans = 'NO'\n\tprint(ans)\n", "import re\nfor i in range(int(input())):\n\tl = int(input())\n\tstr1 = input()\n\tsum1 = 0\n\tsum2 = 0\n\tif str1[0] == '1':\n\t\tprint('YES')\n\telse:\n\t\tfor i in str1:\n\t\t\tif i == '0':\n\t\t\t\tsum1 = sum1 + 1\n\t\t\telse:\n\t\t\t\tsum2 = sum2 + 1\n\t\t\tif sum2 >= sum1:\n\t\t\t\tprint('YES')\n\t\t\t\tbreak\n\t\t\telif sum2 + sum1 == len(str1):\n\t\t\t\tprint('NO')\n", "t = int(input())\nfor i in range(t):\n\tl = int(input())\n\ts = input()\n\tflag = 0\n\t(count1, count2) = (0, 0)\n\tfor i in s:\n\t\tif i == '0':\n\t\t\tcount1 += 1\n\t\telse:\n\t\t\tcount2 += 1\n\t\tif count2 >= count1:\n\t\t\tflag = 1\n\t\t\tbreak\n\tif flag:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nwhile t != 0:\n\tn = int(input())\n\ts = input()\n\tc = 0\n\tf = 0\n\tfor k in range(n):\n\t\tif s[k] == '1':\n\t\t\tc += 1\n\t\t\tif c >= (k + 1) / 2:\n\t\t\t\tf = 1\n\t\t\t\tbreak\n\tif f == 1:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n\tt -= 1\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\t(o, z) = (0, 0)\n\tc = 'NO'\n\tfor i in s:\n\t\tif i == '0':\n\t\t\tz += 1\n\t\telse:\n\t\t\to += 1\n\t\t\tif o >= z:\n\t\t\t\tc = 'YES'\n\t\t\t\tbreak\n\tprint(c)\n", "for _ in range(int(input())):\n\tl = int(input())\n\ts = input()\n\tz = 0\n\to = 0\n\tans = 'NO'\n\tfor c in s:\n\t\tif c == '0':\n\t\t\tz += 1\n\t\telse:\n\t\t\to += 1\n\t\t\tif o >= z:\n\t\t\t\tans = 'YES'\n\t\t\t\tbreak\n\tprint(ans)\n", "for i in range(int(input())):\n\ta = int(input())\n\ts = input()\n\tc = d = 0\n\tfor i in range(len(s)):\n\t\tif s[i] == '1':\n\t\t\tc += 1\n\t\tif c * 100 / (i + 1) >= 50:\n\t\t\tprint('YES')\n\t\t\tbreak\n\telse:\n\t\tprint('NO')\n", "import math\nfor i in range(int(input())):\n\tn = int(input())\n\ts = input()\n\tc = 0\n\tif s.count('1') >= s.count('0'):\n\t\tprint('YES')\n\telse:\n\t\to = 0\n\t\tz = 0\n\t\tfor i in s:\n\t\t\tif i == '1':\n\t\t\t\to += 1\n\t\t\telse:\n\t\t\t\tz += 1\n\t\t\tif o == z:\n\t\t\t\tc += 1\n\t\t\t\tprint('YES')\n\t\t\t\tbreak\n\t\tif c == 0:\n\t\t\tprint('NO')\n", "t = int(input())\nfor _ in range(t):\n\tl = int(input())\n\ts = input()\n\tcntz = 0\n\tcnto = 0\n\tt = True\n\tfor i in range(l):\n\t\tif s[i] == '0':\n\t\t\tcntz += 1\n\t\tif s[i] == '1':\n\t\t\tcnto += 1\n\t\tif cntz == cnto:\n\t\t\tt = False\n\t\t\tbreak\n\tif cntz < cnto or not t:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "T = int(input())\nfor _ in range(T):\n\tL = int(input())\n\tS = input()\n\tzeros = 0\n\tones = 0\n\tflag = False\n\tfor i in S:\n\t\tif i == '1':\n\t\t\tones += 1\n\t\telse:\n\t\t\tzeros += 1\n\t\tif ones >= zeros:\n\t\t\tflag = True\n\t\t\tbreak\n\tprint('YES' if flag else 'NO')\n", "for i in range(int(input())):\n\ta = int(input())\n\tb = input()\n\tch = 0\n\tbj = 0\n\tfor i in range(1, a + 1):\n\t\tif b[i - 1] == '1':\n\t\t\tch += 1\n\t\tif ch / i >= 0.5:\n\t\t\tbj += 1\n\t\t\tbreak\n\tif bj == 1:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nfor k in range(t):\n\tn = int(input())\n\ta = str(input())\n\tb = 0\n\tc = 0\n\td = 0\n\twhile b != len(a):\n\t\tif a[b] == '0':\n\t\t\tc += 1\n\t\telse:\n\t\t\td += 1\n\t\tb += 1\n\t\tif c <= d:\n\t\t\tb = len(a)\n\tif c <= d:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "T = int(input())\nfor i in range(T):\n\tL = int(input())\n\tS = input()\n\tc = 0\n\tli = []\n\tfor j in S:\n\t\tli.append(j)\n\tfor i in range(L):\n\t\tif li[i] == '1':\n\t\t\tc = c + 1\n\t\tif c * 100 / (i + 1) >= 50:\n\t\t\tz = c * 100 / (i + 1)\n\t\t\tbreak\n\t\telse:\n\t\t\tz = c * 100 / (i + 1)\n\tprint('YES' if z >= 50 else 'NO')\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\tone = 0\n\tchk = 0\n\tfor i in range(1, n + 1):\n\t\tif s[i - 1] == '1':\n\t\t\tone += 1\n\t\tif one / i >= 0.5:\n\t\t\tchk = 1\n\t\t\tbreak\n\tif chk == 1:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "for i in range(int(input())):\n\tl = int(input())\n\ts = input()\n\tgood = s.count('1')\n\tbad = s.count('0')\n\tif good >= len(s) / 2:\n\t\tprint('YES')\n\telse:\n\t\t(cb, cg, flag) = (0, 0, False)\n\t\tfor i in s:\n\t\t\tif i == '1':\n\t\t\t\tcg += 1\n\t\t\telse:\n\t\t\t\tcb += 1\n\t\t\tif cg >= cb:\n\t\t\t\tprint('YES')\n\t\t\t\tflag = True\n\t\t\t\tbreak\n\t\tif flag == False:\n\t\t\tprint('NO')\n", "t = int(input())\nfor i in range(t):\n\tl = int(input())\n\ts = input()\n\tc = 0\n\td = 0\n\tans = False\n\tfor i in s:\n\t\tif i == '0':\n\t\t\tc += 1\n\t\telse:\n\t\t\td += 1\n\t\tif c <= d:\n\t\t\tans = True\n\t\t\tbreak\n\tif ans:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\theaven = False\n\tone = 0\n\ttotal = 0\n\tfor i in s:\n\t\tif i == '1':\n\t\t\tone += 1\n\t\t\ttotal += 1\n\t\t\tif 2 * one >= total:\n\t\t\t\theaven = True\n\t\t\t\tbreak\n\t\telse:\n\t\t\ttotal += 1\n\tif heaven:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nfor i in range(t):\n\tn = int(input())\n\tst = input()\n\tg = 0\n\tb = 0\n\td = 0\n\tfor i in st:\n\t\tif i == '0':\n\t\t\tb = b + 1\n\t\telse:\n\t\t\tg = g + 1\n\t\tif g >= b:\n\t\t\tprint('YES')\n\t\t\td = d + 1\n\t\t\tbreak\n\tif d == 0:\n\t\tprint('NO')\n", "for _ in range(int(input())):\n\tl = int(input())\n\ts = input()\n\tc = 0\n\tc1 = 0\n\tx = 0\n\tfor i in range(l):\n\t\tif s[i] == '0':\n\t\t\tc += 1\n\t\telse:\n\t\t\tc1 += 1\n\t\tif c1 >= c:\n\t\t\tx = i + 1\n\t\t\tbreak\n\tif x == 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n", "q = int(input())\nwhile q > 0:\n\tq -= 1\n\tn = int(input())\n\tl = input()\n\tcount = 0\n\tflag = 0\n\tfor i in range(0, n):\n\t\tif l[i] == '1':\n\t\t\tcount += 1\n\t\tif count / (i + 1) * 100 >= 50:\n\t\t\tflag = 1\n\t\t\tbreak\n\tif flag == 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n", "for t in range(int(input())):\n\tl = input()\n\ts = input()\n\tone = 0\n\tzero = 0\n\tc = 0\n\tfor i in s:\n\t\tif i == '1':\n\t\t\tone = one + 1\n\t\telse:\n\t\t\tzero = zero + 1\n\t\tif one >= zero:\n\t\t\tc = c + 1\n\t\t\tprint('YES')\n\t\t\tbreak\n\tif c == 0:\n\t\tprint('NO')\n", "tests = int(input())\nfor i in range(tests):\n\tlength = int(input())\n\tstring = input()\n\tans = 'NO'\n\tzeroes = 0\n\tones = 0\n\tfor j in string:\n\t\tif j == '0':\n\t\t\tzeroes += 1\n\t\telse:\n\t\t\tones += 1\n\t\tif ones >= zeroes:\n\t\t\tans = 'YES'\n\t\t\tbreak\n\tprint(ans)\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\t(a, b) = (0, 0)\n\tfor i in s:\n\t\tif i == '1':\n\t\t\ta += 1\n\t\telse:\n\t\t\tb += 1\n\t\tif a >= b:\n\t\t\tprint('YES')\n\t\t\tbreak\n\telse:\n\t\tprint('NO')\n", "T = int(input())\nfor i in range(T):\n\tn = int(input())\n\tl = str(input())\n\tbad = 0\n\tgood = 0\n\tp = False\n\tfor z in l:\n\t\tif z == '1':\n\t\t\tgood += 1\n\t\telse:\n\t\t\tbad += 1\n\t\tif good >= bad:\n\t\t\tp = True\n\tif p:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "for _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\t(c1, c2, f) = (0, 0, 0)\n\tfor i in s:\n\t\tif i == '0':\n\t\t\tc1 += 1\n\t\telse:\n\t\t\tc2 += 1\n\t\tif c2 >= c1:\n\t\t\tprint('YES')\n\t\t\tf = 1\n\t\t\tbreak\n\tif f == 0:\n\t\tprint('NO')\n", "t = int(input())\nwhile t:\n\tt -= 1\n\tn = int(input())\n\ts = input()\n\tcount0 = 0\n\tcount1 = 0\n\tf = 0\n\tfor i in s:\n\t\tif i == '0':\n\t\t\tcount0 += 1\n\t\telse:\n\t\t\tcount1 += 1\n\t\tif count1 >= count0:\n\t\t\tprint('YES')\n\t\t\tf = 1\n\t\t\tbreak\n\tif f == 0:\n\t\tprint('NO')\n", "for i in range(int(input())):\n\tL = int(input())\n\tS = str(input())\n\tY = 0\n\tflag = False\n\tif S.count('1') >= L / 2:\n\t\tflag = True\n\telse:\n\t\tfor ii in range(L):\n\t\t\tY = Y + int(S[ii])\n\t\t\tif Y / (ii + 1) >= 0.5:\n\t\t\t\tflag = True\n\t\t\t\tbreak\n\tprint('YES') if flag else print('NO')\n", "for _ in range(int(input())):\n\tN = int(input())\n\tS = input()\n\t(One, zero) = (0, 0)\n\tflag = False\n\tfor i in range(N):\n\t\tif S[i] == '0':\n\t\t\tzero += 1\n\t\telse:\n\t\t\tOne += 1\n\t\tif One >= zero:\n\t\t\tflag = True\n\t\t\tbreak\n\tif flag:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nfor i in range(t):\n\tl = int(input())\n\ts = input()\n\th = []\n\thl = []\n\tfor j in s:\n\t\tif j == '0':\n\t\t\thl.append(j)\n\t\telse:\n\t\t\th.append(j)\n\t\tif len(h) >= len(hl):\n\t\t\tprint('YES')\n\t\t\tbreak\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nfor i in range(t):\n\tL = int(input())\n\tS = input()\n\tgood = 0\n\tbad = 0\n\tposs = False\n\tfor i in range(L):\n\t\tif S[i] == '1':\n\t\t\tgood += 1\n\t\telse:\n\t\t\tbad += 1\n\t\tif good >= bad:\n\t\t\tposs = True\n\tif poss:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "for i in range(int(input())):\n\tl = int(input())\n\ts = input()\n\tx = 'NO'\n\tg = 0\n\tb = 0\n\tif s.count('1') >= l / 2:\n\t\tx = 'YES'\n\telse:\n\t\tfor i in s:\n\t\t\tif g >= b and g != 0:\n\t\t\t\tx = 'YES'\n\t\t\t\tbreak\n\t\t\telif i == '1':\n\t\t\t\tg += 1\n\t\t\telse:\n\t\t\t\tb += 1\n\tprint(x)\n", "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tlife = str(input())\n\tcount = 0\n\tif life.count('1') >= life.count('0'):\n\t\tprint('YES')\n\telse:\n\t\ti = 0\n\t\twhile i < n:\n\t\t\tif life[i] == '0':\n\t\t\t\tcount = count - 1\n\t\t\telse:\n\t\t\t\tcount = count + 1\n\t\t\t\tif count >= 0:\n\t\t\t\t\tbreak\n\t\t\ti = i + 1\n\t\tif count >= 0:\n\t\t\tprint('YES')\n\t\telse:\n\t\t\tprint('NO')\n", "for i in range(int(input())):\n\tL = int(input())\n\ts = input()\n\tones = 0\n\tzeros = 0\n\tfor j in range(len(s)):\n\t\tif s[j] == '0':\n\t\t\tzeros += 1\n\t\telse:\n\t\t\tones += 1\n\t\tif ones >= zeros:\n\t\t\tprint('YES')\n\t\t\tbreak\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nwhile t > 0:\n\tl = int(input())\n\ts = input()\n\ti = 0\n\tflg = 0\n\tc0 = 0\n\tc1 = 0\n\twhile i < l:\n\t\tif s[i] == '1':\n\t\t\tc1 += 1\n\t\t\tif c1 * 100 / (i + 1) >= 50:\n\t\t\t\tflg = 1\n\t\t\t\tprint('YES')\n\t\t\t\tbreak\n\t\ti += 1\n\tif flg == 0:\n\t\tprint('NO')\n\tt -= 1\n", "for _ in range(int(input())):\n\tL = int(input())\n\tS = input()\n\tbad = 0\n\tgood = 0\n\tfor i in range(L):\n\t\tif i != 0 and bad <= good:\n\t\t\tbreak\n\t\tif S[i] == '0':\n\t\t\tbad += 1\n\t\telse:\n\t\t\tgood += 1\n\tif bad <= good:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "import math\nfor _ in range(int(input())):\n\tn = int(input())\n\ts = input()\n\tl = 0\n\tc = 0\n\tfor i in range(n):\n\t\tif i != 0 and l <= c:\n\t\t\tbreak\n\t\telif s[i] == '0':\n\t\t\tl += 1\n\t\telse:\n\t\t\tc += 1\n\tif l <= c:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "t = int(input())\nfor i in range(t):\n\tg = 0\n\tb = 0\n\tded = True\n\tl = int(input())\n\ts = input()\n\tfor j in s:\n\t\tded = True\n\t\tif j == '1':\n\t\t\tg += 1\n\t\t\tif g >= b:\n\t\t\t\tprint('YES')\n\t\t\t\tded = False\n\t\t\t\tbreak\n\t\telif j == '0':\n\t\t\tb += 1\n\t\t\tif g >= b:\n\t\t\t\tprint('YES')\n\t\t\t\tded = False\n\t\t\t\tbreak\n\tif ded:\n\t\tprint('NO')\n", "t = int(input())\nwhile t > 0:\n\tn = int(input())\n\tstrg = list(input())\n\tcount_zero = 0\n\tfor i in range(n):\n\t\tvar = 'NO'\n\t\tif strg[i] == '0':\n\t\t\tcount_zero += 1\n\t\tif i + 1 - count_zero >= count_zero:\n\t\t\tvar = 'YES'\n\t\t\tbreak\n\tprint(var)\n\tt -= 1\n", "testcasenum = int(input())\n\ndef heaven(deeds):\n\tgood_deeds = 0\n\tbad_deeds = 0\n\tfor i in range(len(deeds)):\n\t\tif deeds[i] == 1:\n\t\t\tgood_deeds += 1\n\t\telif deeds[i] == 0:\n\t\t\tbad_deeds += 1\n\t\tif good_deeds >= bad_deeds:\n\t\t\treturn 'YES'\n\treturn 'NO'\nfor __ in range(testcasenum):\n\tlife = int(input())\n\tdeeds = list(map(int, list(input())))\n\tresult = heaven(deeds)\n\tprint(result)\n", "n = int(input())\nnum = []\ndt = []\nfor i in range(0, n):\n\tnum.append(int(input()))\n\tdt.append(input())\nfor j in range(0, n):\n\tpas = 0\n\tgd = 0\n\tbd = 0\n\tlst = list(dt[j])\n\tfor k in range(0, num[j]):\n\t\tif lst[k] == '0':\n\t\t\tbd = bd + 1\n\t\telse:\n\t\t\tgd = gd + 1\n\t\tif gd >= bd:\n\t\t\tpas = pas + 1\n\tif pas == 0:\n\t\tprint('NO')\n\telse:\n\t\tprint('YES')\n", "for i in range(int(input())):\n\tl = 0\n\tc = 0\n\tj = int(input())\n\tst = input()\n\tfor i in range(0, len(st)):\n\t\tif i != 0 and l <= c:\n\t\t\tbreak\n\t\telif st[i] == '0':\n\t\t\tl = l + 1\n\t\telse:\n\t\t\tc = c + 1\n\tif l <= c:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n", "for _ in range(int(input())):\n\ta = int(input())\n\tb = input()\n\tg = 0\n\tp = 0\n\tk = 0\n\tfor i in b:\n\t\tif i == '1':\n\t\t\tg += 1\n\t\tif i == '0':\n\t\t\tp += 1\n\t\tif g >= p:\n\t\t\tprint('YES')\n\t\t\tk = 1\n\t\t\tbreak\n\tif k == 0:\n\t\tprint('NO')\n", "T = int(input())\nfor i in range(T):\n\tL = int(input())\n\tS = input()\n\tcount = 0\n\tcount1 = 0\n\tfor j in S:\n\t\tif int(j) == 1:\n\t\t\tcount += 1\n\t\telse:\n\t\t\tcount1 += 1\n\t\tif count >= count1:\n\t\t\tprint('YES')\n\t\t\tbreak\n\tif count1 > count:\n\t\tprint('NO')\n", "t = int(input())\nwhile t > 0:\n\tl = int(input())\n\ts = input()\n\tcountz = 0\n\tcounto = 0\n\tfor i in range(0, len(s)):\n\t\tif s[i] == '0':\n\t\t\tcountz = countz + 1\n\t\telse:\n\t\t\tcounto = counto + 1\n\t\tif counto >= countz:\n\t\t\tprint('YES')\n\t\t\tbreak\n\tif countz > counto:\n\t\tprint('NO')\n\tt = t - 1\n", "def main():\n\tfor _ in range(int(input())):\n\t\tL = int(input())\n\t\tS = input()\n\t\t(gy, by) = (0, 0)\n\t\tans = 'NO'\n\t\tfor i in range(L):\n\t\t\tif S[i] == '1':\n\t\t\t\tgy += 1\n\t\t\telse:\n\t\t\t\tby += 1\n\t\t\tif gy >= by:\n\t\t\t\tans = 'YES'\n\t\t\t\tbreak\n\t\tprint(ans)\nmain()\n", "t = int(input())\nfor i in range(t):\n\tL = int(input())\n\tS = input()\n\ttotal1 = 0\n\ttotal2 = 0\n\tflag = 0\n\tfor i in range(L):\n\t\tif S[i] == '0':\n\t\t\ttotal1 += 1\n\t\telse:\n\t\t\ttotal2 += 1\n\t\t\tif total2 >= total1:\n\t\t\t\tflag = 1\n\t\t\t\tprint('YES')\n\t\t\t\tbreak\n\tif flag == 0:\n\t\tprint('NO')\n", "class Person:\n\n\tdef __init__(self, deed):\n\t\tself.deed = deed\n\n\tdef good_deed(self):\n\t\tself.deed += 1\n\n\tdef bad_deed(self):\n\t\tself.deed -= 1\nperson = Person(1)\nt = int(input())\nwhile t:\n\tt -= 1\n\tn = int(input())\n\ts = input()\n\tperson.deed = 1\n\tfor i in s:\n\t\tif i == '0':\n\t\t\tperson.bad_deed()\n\t\telse:\n\t\t\tperson.good_deed()\n\t\tif person.deed > 0:\n\t\t\tbreak\n\tif person.deed > 0:\n\t\tprint('YES')\n\telse:\n\t\tprint('NO')\n" ]
{"inputs": ["3\n2\n10\n3\n001\n4\n0100"], "outputs": ["YES\nNO\nYES"]}
EASY
['Algorithms', 'Greedy']
null
codechef
['Greedy algorithms']
['Greedy algorithms']
https://www.codechef.com/problems/CCHEAVEN
null
0.5 seconds
2021-04-02T00:00:00
0
50000 bytes
null
15
You are given an array A of N integers A_{1}, A_{2}, \ldots, A_{N}. Determine if there are two [permutations] B and C of this array, for which the following condition is satisfied: * There doesn't exist a pair of integers (i, j) such that 1 ≀ i ≀ j ≀ N and (i, j) \neq (1, N), for which the subarray B[i:j] is a permutation of subarray C[i:j]. If there exist such permutations, find any of them. As a reminder, B[i:j] refers to the subarray [B_{i}, B_{i+1}, \ldots, B_{j}] ------ Input Format ------ - The first line of the input contains a single integer T, the number of test cases. The description of the test cases follows. - The first line of each test case contains a single integer N β€” the number of integers. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}. ------ Output Format ------ For each test case, if there are no such permutations B and C, output NO. Otherwise, on the first line output YES. In the next line, output N integers B_{1}, B_{2}, \ldots, B_{N}. In the next line, output N integers C_{1}, C_{2}, \ldots, C_{N}. You may print each character of YES/NO in either uppercase or lowercase (for example, the strings YES, yeS, YeS, and yEs will all be treated as identical). ------ Constraints ------ $1 ≀T ≀100$ $3 ≀N ≀1000$ $0 ≀A_{i} ≀10^{9}$ - The sum of $N$ over all test cases doesn't exceed $2000$. ----- Sample Input 1 ------ 3 3 1 1 2 4 19 39 19 84 6 1 2 3 1 2 3 ----- Sample Output 1 ------ NO YES 19 19 39 84 39 84 19 19 YES 1 1 2 2 3 3 2 3 3 1 1 2 ----- explanation 1 ------ Test case $1$: There are $3 \times 3 = 9$ pairs of permutations of the given array. Here's why they're all bad: - If $B = [1, 1, 2]$ and $C = [1, 1, 2]$, $B[1:1] = C[1:1]$ - If $B = [1, 1, 2]$ and $C = [1, 2, 1]$, $B[1:1] = C[1:1]$ - If $B = [1, 1, 2]$ and $C = [2, 1, 1]$, $B[2:2] = C[2:2]$ - If $B = [1, 2, 1]$ and $C = [1, 1, 2]$, $B[1:1] = C[1:1]$ - If $B = [1, 2, 1]$ and $C = [1, 2, 1]$, $B[1:1] = C[1:1]$ - If $B = [1, 2, 1]$ and $C = [2, 1, 1]$, $B[3:3] = C[3:3]$ - If $B = [2, 1, 1]$ and $C = [1, 1, 2]$, $B[2:2] = C[2:2]$ - If $B = [2, 1, 1]$ and $C = [1, 2, 1]$, $B[3:3] = C[3:3]$ - If $B = [2, 1, 1]$ and $C = [2, 1, 1]$, $B[1:1] = C[1:1]$
[ "t = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tl = list(map(int, input().split()))\n\td = {}\n\tfor i in l:\n\t\td[i] = d.get(i, 0) + 1\n\tmx = 0\n\tfor i in d.values():\n\t\tif i >= mx:\n\t\t\tmx = i\n\tif len(d) == 2:\n\t\tprint('NO')\n\telif n % 2 == 0:\n\t\tif mx <= n // 2:\n\t\t\tprint('YES')\n\t\t\tl.sort()\n\t\t\tfor i in range(n):\n\t\t\t\tprint(l[i], end=' ')\n\t\t\tprint()\n\t\t\tfor i in range(n // 2, n):\n\t\t\t\tprint(l[i], end=' ')\n\t\t\tprint(end='')\n\t\t\tfor i in range(0, n // 2):\n\t\t\t\tprint(l[i], end=' ')\n\t\t\tprint()\n\t\telse:\n\t\t\tprint('NO')\n\telif mx < n // 2 + 1:\n\t\tprint('YES')\n\t\tl.sort()\n\t\tfor i in range(n):\n\t\t\tprint(l[i], end=' ')\n\t\tprint()\n\t\tfor i in range(n // 2 + 1, n):\n\t\t\tprint(l[i], end=' ')\n\t\tprint(end='')\n\t\tfor i in range(0, n // 2 + 1):\n\t\t\tprint(l[i], end=' ')\n\t\tprint()\n\telse:\n\t\tprint('NO')\n", "for _ in range(int(input())):\n\tn = int(input())\n\tl = input().split()\n\td = {}\n\tfor i in l:\n\t\ti = int(i)\n\t\tif i in d:\n\t\t\td[i] += 1\n\t\telse:\n\t\t\td[i] = 1\n\tl = []\n\tfor i in d:\n\t\tl.append([i, d[i]])\n\tif len(l) == 1:\n\t\tprint('NO')\n\telif len(l) == 2:\n\t\tif l[0][0] == l[1][0] and (l[0][1] == 1 or l[0][1] == 2):\n\t\t\tprint('YES')\n\t\t\ts = str(l[0][0]) * l[0][1] + str(l[1][0]) * l[0][1]\n\t\telse:\n\t\t\tprint('NO')\n\telse:\n\t\tle = n // 2\n\t\tma = l[0][1]\n\t\ts = []\n\t\tfor i in l:\n\t\t\ts[len(s):] = [i[0]] * i[1]\n\t\t\tif i[1] > ma:\n\t\t\t\tma = i[1]\n\t\tif ma > le:\n\t\t\tprint('NO')\n\t\telse:\n\t\t\tprint('YES')\n\t\t\tfor i in s:\n\t\t\t\tprint(i, end=' ')\n\t\t\tprint()\n\t\t\ts = s[le:] + s[0:le]\n\t\t\tfor i in s:\n\t\t\t\tprint(i, end=' ')\n\t\t\tprint()\n", "def Print(arr):\n\tfor ele in arr:\n\t\tprint(ele, end=' ')\n\tprint()\nt = int(input())\nfor _ in range(t):\n\tn = int(input())\n\tarr = list(map(int, input().split()))\n\tarr.sort()\n\tar = arr[n // 2:]\n\tfor i in range(n // 2):\n\t\tar.append(arr[i])\n\tans = True\n\tfor i in range(n):\n\t\tif arr[i] == ar[i]:\n\t\t\tans = False\n\t\t\tbreak\n\td = {}\n\tfor ele in arr:\n\t\td[ele] = d.get(ele, 0) + 1\n\tif len(d) == 2:\n\t\ttm = []\n\t\tfor ele in d:\n\t\t\ttm.append(d[ele])\n\t\tif tm[0] == tm[1]:\n\t\t\tans = False\n\tif ans:\n\t\tprint('YES')\n\t\tPrint(arr)\n\t\tPrint(ar)\n\telse:\n\t\tprint('NO')\n" ]
{"inputs": ["3\n3\n1 1 2\n4\n19 39 19 84\n6\n1 2 3 1 2 3"], "outputs": ["NO\nYES\n19 19 39 84 \n39 84 19 19 \nYES\n1 1 2 2 3 3 \n2 3 3 1 1 2 "]}
HARD
['shift', 'sorting', 'trygub_adm', 'cook141', 'constructive']
null
codechef
['Sorting', 'Constructive algorithms']
['Sorting']
https://www.codechef.com/problems/DIFSUBARRAYS
null
1 seconds
2022-04-26T00:00:00
0
50000 bytes
null
18
"You are given an array $a_1, a_2, \\dots, a_n$. You can perform the following operation any number (...TRUNCATED)
["from collections import defaultdict, deque\nfrom heapq import heappush, heappop\nfrom bisect impor(...TRUNCATED)
"{\"inputs\": [\"5\\n4 3 2 2 3\\n\", \"7\\n3 3 4 4 4 3 3\\n\", \"3\\n1 3 5\\n\", \"1\\n1000\\n\", \"(...TRUNCATED)
HARD
['greedy', 'dp']
null
codeforces
['Dynamic programming', 'Greedy algorithms']
['Dynamic programming', 'Greedy algorithms']
https://codeforces.com/problemset/problem/1312/E
null
2 seconds
2020-03-09T00:00:00
0
256 megabytes
null
20
"There are players standing in a row each player has a digit written on their T-Shirt (multiple play(...TRUNCATED)
["import sys\n\ndef GRIG(L):\n\tLENT = len(L)\n\tMINT = 1\n\tGOT = 0\n\tDY = [[{x: 0 for x in range((...TRUNCATED)
{"inputs": [["1", "123343"]], "outputs": [["3"]]}
UNKNOWN_DIFFICULTY
[]
null
codechef
[]
[]
https://www.codechef.com/COVO2020/problems/GRIG
null
null
null
null
null
null

Introduction

This dataset contains verified solutions from the TACO dataset's training set. Solutions that fail to pass all the test cases are removed. Problems with no correct solution are also removed.

The solutions were executed on Intel E5-2620 v3 CPUs with the execution timeout set to 10 seconds.

Statistics in the training set

Dataset # Problems # Solutions
TACO 25443 1468722
TACO-verified 12898 1043251
Correct Ratio 50.69 % 71.03 %
Downloads last month
9
Edit dataset card